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

标签 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”除外)。

任何帮助将不胜感激。

最佳答案

您可以尝试使用 nextLine() 来获取字符串,而不是使用 Scanner.nextInt() 方法。然后你可以检查该字符串是否等于“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");

            }

关于java - 如何通过在 Try-Catch While 循环中输入特定字母来退出程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754786/

相关文章:

Java Try Catch finally 没有 Catch 的 block

java - 从 try/catch block 中返回变量

java - java的赋值问题

java - Jenkins 的密码加密

c - 在没有主体的 While 循环中从 FIFO(命名管道)读取

php - While 循环过滤器类别

java - 使用 ANT 脚本生成 Hibernate POJO 类

Java多线程 - 一个线程在另一个线程之前两次进入临界区

matlab - 通过矩形的随机路径

java - 捕获异常和错误