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

时间:2019-04-18 23:12:26

标签: java while-loop try-catch exit

基本上,我想在用户输入字母“ 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”除外)。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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");

            }