如何在此代码中使用序列。可实现2次?
int input;
int choices;
void setup() {
Serial.begin(9600);
Serial.println("BIN, DEC, HEX, OCT Converter\n");
retry:
Serial.println("Kindly choose a number system to convert (1, 2, 3, 4)");
Serial.println("1. BIN\n2. DEC\n3. HEX\n4. OCT\n");
while (Serial.available() == 0) {}
choices = Serial.parseInt();
if (choices == 1) {
Serial.println("You have chosen BIN, please enter a valid input");
}
else if (choices == 2) {
Serial.println("You have chosen DEC, please enter a valid input");
}
else if (choices == 3) {
Serial.println("You have choses HEX, please enter a valid input");
}
else if (choices == 4) {
Serial.println("You have chosen OCT, please enter a valid input");
}
else {
Serial.println("Invalid choice."); \
goto retry;
}
switch (choices) {
case 1: while (Serial.available() == 0) {}
input = Serial.parseInt();
Serial.print("You have input ");
Serial.print(input);
break;
case 2: while (Serial.available() == 0) {}
input = Serial.parseInt();
Serial.print("You have input ");
Serial.print(input);
break;
}
}
void loop() {
}
在 switch
部分中,当我运行代码时,它只显示 0,并且没有给我再次进入串行监视器的选项。
我似乎无法弄清楚这一点,我是一个初学者,正在尝试改进 Arduino IDE 的语义。谢谢
int input;
int choices;
void setup() {
Serial.begin(9600);
Serial.println("BIN, DEC, HEX, OCT Converter\n");
retry:
Serial.println("Kindly choose a number system to convert (1, 2, 3, 4)");
Serial.println("1. BIN\n2. DEC\n3. HEX\n4. OCT\n");
while (Serial.available() == 0) {}
choices = Serial.parseInt();
if (choices == 1) {
Serial.println("You have chosen BIN, please enter a valid input");
}
else if (choices == 2) {
Serial.println("You have chosen DEC, please enter a valid input");
}
else if (choices == 3) {
Serial.println("You have choses HEX, please enter a valid input");
}
else if (choices == 4) {
Serial.println("You have chosen OCT, please enter a valid input");
}
else {
Serial.println("Invalid choice."); \
goto retry;
}
switch (choices) {
case 1: while (Serial.available() == 0) {}
input = Serial.parseInt();
Serial.print("You have input ");
Serial.print(input);
break;
case 2: while (Serial.available() == 0) {}
input = Serial.parseInt();
Serial.print("You have input ");
Serial.print(input);
break;
}
}
void loop() {
}
In the part switch
, when I run the code, it shows only 0 and it doesn't give me the option to enter at the serial monitor again.
I can't seem to figure this out, I'm a beginner who's trying to improve in the semantics of Arduino IDE. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论