Do-while 循环不会回头

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

Do-while doesn't loop back

问题

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

System.out.println("此程序将二进制值转换为其十进制对应值。\n");
Scanner input = new Scanner(System.in);
boolean invalidInput = true;

do {
    try {
        System.out.print("(>)请输入要转换的值:");
        String numberToConvert = input.nextLine();
        long converted = Long.parseLong(numberToConvert, 2);
        System.out.println("(>)" + numberToConvert + " 的十进制值为:" + converted + "\n");
        invalidInput = false;
    } catch (Exception e) {
        System.out.println("(!)输入的值不是二进制,请重试。\n");
        input.next();
    }
} 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.

System.out.println("This program converts a binary value to its decimal counterpart.\n");
    Scanner input = new Scanner(System.in);
    boolean invalidInput = true;

    do {

        try {
            System.out.print("(>) Enter the value to be converted: ");
            String numberToConvert = input.nextLine();
            long converted = Long.parseLong(numberToConvert, 2);
            System.out.println("(>) Decimal value of " + numberToConvert + " is : " + converted + "\n");
            invalidInput = false;
        } catch (Exception e) {
            System.out.println("(!) Entered value is non binary, please try again.\n");
            input.next();
        }

    } while (invalidInput);

答案1

得分: 1

System.out.println("此程序将二进制值转换为其十进制对应值。\n");
Scanner input = new Scanner(System.in);
boolean invalidInput = true;

do {
    try {
        System.out.print("(>)请输入要转换的值:");
        String numberToConvert = input.nextLine();
        long converted = Long.parseLong(numberToConvert, 2);
        System.out.println("(>)" + numberToConvert + " 的十进制值为:" + converted + "\n");
        invalidInput = false;
    } catch (Exception e) {
        System.out.println("(!)输入的值非二进制,请重试。\n");
    }
} while (invalidInput);
英文:

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

	System.out.println("This program converts a binary value to its decimal counterpart.\n");
    Scanner input = new Scanner(System.in);
    boolean invalidInput = true;

    do {

        try {
            System.out.print("(>) Enter the value to be converted: ");
            String numberToConvert = input.nextLine();
            long converted = Long.parseLong(numberToConvert, 2);
            System.out.println("(>) Decimal value of " + numberToConvert + " is : " + converted + "\n");
            invalidInput = false;
        } catch (Exception e) {
            System.out.println("(!) Entered value is non binary, please try again.\n");
        }

    } 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:

确定