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

Java 如何在Try-Catch-while循环中输入特定字母退出程序?,java,while-loop,try-catch,exit,Java,While Loop,Try Catch,Exit,基本上,当用户输入字母“q”而不是整数时,我想退出程序 已经尝试了几个小时,试图通过添加 if(quit.equalsIgnoreCase("q")){ System.exit(0) } 在try语句中。尝试从try语句中删除扫描程序并将其添加到while循环之前,然后创建一个新变量,如下所示: String quit = ""; while (quit != "q"){ //code } 然后又在代码中添加了一种退出的方法,但这不起作用 while (true) {

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

已经尝试了几个小时,试图通过添加

if(quit.equalsIgnoreCase("q")){
System.exit(0)
}
在try语句中。尝试从try语句中删除扫描程序并将其添加到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(您的字符串)
将字符串解析为整数。但是,如果用户输入的不是数字或“q”,则可能导致NumberFormatException

while(true){
试一试{
int randomNumberOne=ThreadLocalRandom.current().nextInt(10,21);//生成一个介于10和20之间的数字
int randomNumberTwo=ThreadLocalRandom.current().nextInt(10,21);//生成一个介于10和20之间的数字
System.out.println(“键入并输入\“q\”随时退出\n\n”);
System.out.println(randomNumberOne+“%”“%”+randomNumberWO+“=?”;//打印问题
扫描仪用户输入=新扫描仪(System.in);
字符串remainderInputStr=userInput.nextLine();
if(remainderInputStr.equalsIgnoreCase(“q”)){
系统出口(0);
}
int remainderInput=Integer.parseInt(remainderInputStr);
如果(remainderInput==randomNumberOne%randomNumberTwo){//如果他们答对了问题
userScore+=20;//加20分
性能+=1;//将1添加到正确的问题计数器
performancetotal+=1;//向总问题计数器中添加1
System.out.println(“正确答案,当前分数:“+userScore+”,性能:“+performance+”/“+performancetotal+”\n”);
继续;
}否则{//如果他们答错了问题
userScore+=0;//不添加任何分数
性能+=0;//不向正确的问题计数器添加任何内容
性能总计+=1;
System.out.println(“回答不正确,当前分数:+userScore+”,性能:+performance+”/“+performancetotal+”\n”);
继续;
}
}捕获(数字格式){
System.out.println(“无效输入\n”);
}

该程序将永远运行,直到用户输入“q”,然后它将停止运行。这不是重点吗?问题出在哪里?@shmosel抱歉。意思是“该程序应该永远运行”。我只是试图解释它的功能。问题是我找不到方法让它退出atm。显然,我尝试过System.exit(0)之类的东西你当前的代码段没有一个
中断;
语句或
while
条件,那么你为什么希望它停止呢?你想设置一个标志(布尔值是好的),当输入q时设置该标志,并对照该标志检查while循环。