如何通过在循环中输入特定字母来退出程序



基本上,当用户输入字母"q"而不是整数时,我想退出程序。

已经尝试了几个小时,试图通过添加来解决它

if(quit.equalsIgnoreCase("q")){
System.exit(0)
}

在 try 语句中。尝试从 try 语句中删除 Scanner 并将其添加到 while 循环之前,然后创建一个新变量,如下所示:

String quit = "";
while (quit != "q"){
      //code
}

然后再次在代码中添加一种稍后退出的方法,但这不起作用。

 while (true) {
                try {
                    int randomNumberOne = ThreadLocalRandom.current().nextInt(10, 21); //generates a number between 10 and 20
                    int randomNumberTwo = ThreadLocalRandom.current().nextInt(10, 21); //generates a number between 10 and 20
                    System.out.println("Type and enter "q" to quit at any time n n");
                    System.out.println(randomNumberOne + " % " + randomNumberTwo + " = ?"); //prints the question
                    Scanner userInput = new Scanner(System.in);
                    int remainderInput = userInput.nextInt();
                    if (remainderInput == randomNumberOne % randomNumberTwo) { //if they get the question right
                        userScore += 20; //adds 20 points
                        performance += 1; //adds 1 to the correct question counter
                        performancetotal += 1; //adds 1 to the total question counter
                        System.out.println("Correct answer, Current Score: " + userScore + ", performance: " + performance + "/" + performancetotal + "n");
                        continue;
                    }
                    else { //if they get the question wrong
                        userScore += 0; //adds no points
                        performance += 0; //adds nothing to correct question counter
                        performancetotal += 1;
                        System.out.println("Incorrect answer, Current Score: " + userScore + ", performance: " + performance + "/" + performancetotal + "n");
                        continue;
                    }
                 }
                catch (InputMismatchException e) {
                    System.out.println("Invalid inputn");
                }
         }
    }

这是我当前的代码,除了顶部的一些变量,它不应该影响代码。

该程序应该永远运行,直到用户输入"q",然后它将停止运行。try/catch 语句在那里,因此它们只能输入整数(当然"q"除外(。

任何帮助将不胜感激。

与其使用

Scanner.nextInt() 方法,不如尝试使用 nextLine() 来获取字符串。然后,您可以检查该字符串是否等于"q"。如果没有,您可以使用 Integer.parseInt(yourString) 将字符串解析为整数。但是,如果用户输入数字或"q"以外的任何内容,则可能导致 NumberFormatException。

while (true) {
            try {
                int randomNumberOne = ThreadLocalRandom.current().nextInt(10, 21); //generates a number between 10 and 20
                int randomNumberTwo = ThreadLocalRandom.current().nextInt(10, 21); //generates a number between 10 and 20
                System.out.println("Type and enter "q" to quit at any time n n");
                System.out.println(randomNumberOne + " % " + randomNumberTwo + " = ?"); //prints the question
                Scanner userInput = new Scanner(System.in);
                String remainderInputStr = userInput.nextLine();
                if (remainderInputStr.equalsIgnoreCase("q")) {
                    System.exit(0);
                }
                int remainderInput = Integer.parseInt(remainderInputStr);
                if (remainderInput == randomNumberOne % randomNumberTwo) { //if they get the question right
                    userScore += 20; //adds 20 points
                    performance += 1; //adds 1 to the correct question counter
                    performancetotal += 1; //adds 1 to the total question counter
                    System.out.println("Correct answer, Current Score: " + userScore + ", performance: " + performance + "/" + performancetotal + "n");
                    continue;
                } else { //if they get the question wrong
                    userScore += 0; //adds no points
                    performance += 0; //adds nothing to correct question counter
                    performancetotal += 1;
                    System.out.println("Incorrect answer, Current Score: " + userScore + ", performance: " + performance + "/" + performancetotal + "n");
                    continue;
                }
            } catch (NumberFormatException e) {
                System.out.println("Invalid inputn");
            }

相关内容

最新更新