Do-while 循环不会回头

huangapple go评论103阅读模式
英文:

Do-while doesn't loop back

问题

以下是翻译好的代码部分:

  1. System.out.println("此程序将二进制值转换为其十进制对应值。\n");
  2. Scanner input = new Scanner(System.in);
  3. boolean invalidInput = true;
  4. do {
  5. try {
  6. System.out.print("(>)请输入要转换的值:");
  7. String numberToConvert = input.nextLine();
  8. long converted = Long.parseLong(numberToConvert, 2);
  9. System.out.println("(>)" + numberToConvert + " 的十进制值为:" + converted + "\n");
  10. invalidInput = false;
  11. } catch (Exception e) {
  12. System.out.println("(!)输入的值不是二进制,请重试。\n");
  13. input.next();
  14. }
  15. } while (invalidInput);
英文:

So I'm working on a that involves binary conversion etc. But my problem here is that I can't seem to make the do-while statement in my code to loopback whenever the catch block finishes executing. Here's my code.

  1. System.out.println("This program converts a binary value to its decimal counterpart.\n");
  2. Scanner input = new Scanner(System.in);
  3. boolean invalidInput = true;
  4. do {
  5. try {
  6. System.out.print("(>) Enter the value to be converted: ");
  7. String numberToConvert = input.nextLine();
  8. long converted = Long.parseLong(numberToConvert, 2);
  9. System.out.println("(>) Decimal value of " + numberToConvert + " is : " + converted + "\n");
  10. invalidInput = false;
  11. } catch (Exception e) {
  12. System.out.println("(!) Entered value is non binary, please try again.\n");
  13. input.next();
  14. }
  15. } while (invalidInput);

答案1

得分: 1

  1. System.out.println("此程序将二进制值转换为其十进制对应值。\n");
  2. Scanner input = new Scanner(System.in);
  3. boolean invalidInput = true;
  4. do {
  5. try {
  6. System.out.print("(>)请输入要转换的值:");
  7. String numberToConvert = input.nextLine();
  8. long converted = Long.parseLong(numberToConvert, 2);
  9. System.out.println("(>)" + numberToConvert + " 的十进制值为:" + converted + "\n");
  10. invalidInput = false;
  11. } catch (Exception e) {
  12. System.out.println("(!)输入的值非二进制,请重试。\n");
  13. }
  14. } while (invalidInput);
英文:

Remove input.next() from catch block and then try :-

  1. System.out.println("This program converts a binary value to its decimal counterpart.\n");
  2. Scanner input = new Scanner(System.in);
  3. boolean invalidInput = true;
  4. do {
  5. try {
  6. System.out.print("(>) Enter the value to be converted: ");
  7. String numberToConvert = input.nextLine();
  8. long converted = Long.parseLong(numberToConvert, 2);
  9. System.out.println("(>) Decimal value of " + numberToConvert + " is : " + converted + "\n");
  10. invalidInput = false;
  11. } catch (Exception e) {
  12. System.out.println("(!) Entered value is non binary, please try again.\n");
  13. }
  14. } while (invalidInput);

huangapple
  • 本文由 发表于 2020年10月18日 15:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64410605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定