【问题标题】:What is wrong with this java while loop?这个 java while 循环有什么问题?
【发布时间】:2020-09-13 19:30:07
【问题描述】:

Java 新手,学习如何使用 While 循环和随机生成器。这将打印一个乘法问题。每次用户回答错误的问题时,它都应该打印相同的问题。相反,它退出程序。我该怎么办?

while (true) {
    Random multiply = new Random();
    int num1 = multiply.nextInt(15);
    int num2 = multiply.nextInt(15);
    int output = num1 * num2;

    System.out.println("What is the answer to " + num1 + " * " + num2);

    Scanner input = new Scanner(System.in);
    int answer = input.nextInt();
    if (answer == output) {
        if (answer != -1)
            System.out.println("Very good!");
    } else {
        System.out.println("That is incorrect, please try again.");
    }
}

【问题讨论】:

  • 你应该打破不正确答案的循环,比如if(answer == output){System.out.println("Very good!");} else {System.out.println("That is incorrect");break;}
  • 编辑后问题的上下文略有变化,但当我运行它时它仍然不只是退出。
  • 对不起,我问错了问题,今天已经很长了。我真正需要发生的是,如果用户输入错误的答案,循环再次询问相同的问题,它应该打印“不正确,再试一次”,然后允许用户回答相同的问题,而不是打印新的问题直到正确回答该问题。
  • 然后继续在循环中输入输入,如果正确则中断,如while(true){int answer = input.nextInt();if(answer == output){System.out.println("Very good!");break;} else {System.out.println("That is incorrect");}}

标签: java random while-loop


【解决方案1】:

如果您想在用户回答错误时重复相同的问题,您应该在主循环中使用另一个 while

只要你给出错误的答案,这个内部循环就会继续询问。

我还将nextInt 替换为nextLine,它会读入整行文本。这会消耗“Enter”键,并且是从控制台读取的更安全的方法。由于结果现在是String,您需要使用Integer.parseInt 将其转换为int。如果您输入除整数以外的任何内容,则会引发异常,因此我将其包装到 try-catch 块中。

如果需要,您可以添加额外的检查以验证用户输入。所以如果用户想停止播放,他们只需要输入“退出”,整个外循环就会退出。

boolean running = true; // This flag tracks if the program should be running.
while (running) {
    Random multiply = new Random();
    int num1 = multiply.nextInt(15);
    int num2 = multiply.nextInt(15);
    int output = num1 * num2;
    boolean isCorrect = false; // This flag tracks, if the answer is correct

    while (!isCorrect) {
        System.out.println("What is the answer to " + num1 + " * " + num2);

        Scanner input = new Scanner(System.in);
        try {
            String userInput = input.nextLine(); // Better use nextLine to consume the "Enter" key.
            // If the user wants to stop
            if (userInput.equals("exit")) {
                 running = false; // Don't run program any more
                 break;
            }
            int answer = Integer.parseInt(userInput); 
            if (answer == output) {
                if (answer != -1) {
                    System.out.println("Very good!");
                    isCorrect = true; // Set the flag to true, to break out of the inner loop
                }
            } else {
                System.out.println("That is incorrect, please try again.");
            }
        }
        catch(NumberFormatException e) {
            System.out.println("Please enter only whole numbers");
        }
    }
}

【讨论】:

    【解决方案2】:

    当真时避免。将变量声明为 true,将变量传递给 condición 循环,并在答案不正确时将其设置为 false。您也可以使用 break,但是当您在 while 中使用退出条件时更容易阅读代码。另请阅读有关循环的更多信息https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-06
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      相关资源
      最近更新 更多