如何通过在 Try-Catch While 循环中输入特定字母来退出程序?
How to quit program by entering specific letter while inside a Try-Catch While Loop?
基本上我想在用户输入字母 "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",然后它将停止 运行ning。 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");
}
基本上我想在用户输入字母 "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",然后它将停止 运行ning。 try/catch 语句在那里,因此它们只能输入整数(当然 "q" 除外)。
如有任何帮助,我们将不胜感激。
您可以尝试使用 nextLine()
来获取字符串,而不是使用 Scanner.nextInt()
方法。然后您可以检查该字符串是否等于 "q"。如果不是,您可以使用 Integer.parseInt(yourString)
将字符串解析为整数。但是,如果用户输入除数字或 "q".
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");
}