如何通过在Try-Catch While循环中输入特定字母来退出程序?

问题描述 投票:0回答:1

基本上我想在用户输入字母“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 input\n");

                }

         }
    }

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

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

任何帮助将不胜感激。

java while-loop try-catch exit
1个回答
1
投票

您可以尝试使用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 input\n");

            }
© www.soinside.com 2019 - 2024. All rights reserved.