java - 为什么第二个 while 循环取消了两个 while 循环?

标签 java

为什么第二个while循环while (numberOfTries < 2)取消两个 while 循环?如果没有错误答案,则运行完美。但假设我选择了 4 个问题,而我只处理第一个问题。我给出了错误答案两次,所以程序应该说两次错误,然后给我一个新问题,因为 while (numberOfTries < 2)应该迫使它打破这个循环。但事实并非如此,它只是放弃了整个事情。我知道这一定是一个逻辑问题,那么我错过了什么?

import java.util.Random;
import java.util.Scanner;

public class Howthe {

    public static void main(String[] args) {
        // Open Scanner
        Scanner scan = new Scanner(System.in);

        // Ask user to choose number of problems to be made. 
        // Can only choose 4, 9, or 16
        System.out.print("Choose a number of problems to be made (4, 9, 16): ");
        int userChoiceOfProblems = scan.nextInt();

        // Ask user to choose a number between 0 and 12
        System.out.print("\nChoose a number between 0 and 12: ");
        int userNumberBetween0and12 = scan.nextInt();

        // Ask user to choose between add/sub or multiply/divide
        System.out.println("\nChoose to:"
                + "\n0: add/sub  your chosen number"
                + " and the randomly generated number: "
                + "\n1: multiply/divide your chosen number"
                + " and the randomly generated number: ");
        int userArithmeticChoice = scan.nextInt();

        int counter = 0;
        String equationString;
        int equationAnswer;
        int numberOfAnswersRight = 0; 
        int numberOfTries = 0;
        int userAnswerToQuestion;
        if (userArithmeticChoice == 0){
            while (counter < userChoiceOfProblems){
                // Create random number to decide if add or sub used.
                // add is equal to 0 and sub is equal to 1 
                Random rand = new Random();
                int randomNumberBetween0and1 = rand.nextInt(1) + 0;

                // Create random number that is multiplied by userNumberBetween0and12
                int randomNumberBetween0and12 = rand.nextInt(12) + 0;

                // Add and increase counter by 1
                if (randomNumberBetween0and1 == 0){
                    // If numberOfTries is more than 2, then display answer. 
                    while (numberOfTries < 2){
                        // Compute the right answer (addition). 
                        equationAnswer = userNumberBetween0and12 + randomNumberBetween0and12;
                        // Produce string of equation, then display string (addition).
                        equationString = userNumberBetween0and12 + " + "
                                + randomNumberBetween0and12;
                        System.out.println(equationString);
                        userAnswerToQuestion = scan.nextInt();

                        // If answer is right, increase numberOfAnswersRight.
                        if (userAnswerToQuestion == equationAnswer){
                            numberOfAnswersRight++;
                            System.out.println("Correct!");
                            break;
                        }
                        // If answer is wrong, continue loop and increase numberOfTries
                        else if (userAnswerToQuestion != equationAnswer){
                            numberOfTries++;
                            System.out.println("Incorrect");    
                        }   
                    } // end of while (numberOfTries < 2 && !quit)
                    counter++;
                }
            } System.out.println("Yout got " + numberOfAnswersRight + " problem(s) right!");
        }
    }
}

最佳答案

numberOfTries 在循环之外初始化。一旦你尝试两次,它就永远不会被设置回 0,这会导致循环跳过并完成下一个问题,因为 numberOfTries 已经是 2。

关于java - 为什么第二个 while 循环取消了两个 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39859166/

相关文章:

java访问网络摄像头和手写板(独立应用程序与小程序)

java - 从其他类获取 spinner 值并将其分配给 editText

java - 使用 Junit 5 和 Spring Boot 2 在 Mokito 2 中未发现测试

java - 在 Android 上移动 ImageView

java - 使用 JUnit 的 Rule 注释的临时文件夹

java - 更新表时出错 [未为参数 3 指定值]

java - 在java中实现一个事件处理程序并通过jni接口(interface)将其传递给scip

java - Bresenham 的画线算法的错误

java - Android 应用锁库

java - 如果实体已经存在,如何通过属性返回实体?