如何在此代码中使用序列。可实现2次?

发布于 2025-01-17 12:09:43 字数 1220 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文