当用户输入无效值(如字符串或字符)时,程序应忽略它并再次显示上一个问题。到目前为止,我只能在程序捕获无效输入时退出程序。
有效输入是int
和“q”,用户决定退出。
public static void partB() {
int score = 0;
int correct = 0;
int done = 0;
Scanner scan = new Scanner(System.in);
try {
while (true) {
int num1 = (int) (Math.random() * 20);
int num2 = (int) ((Math.random() * 20) + 1);
System.out.printf("%d %% %d = ?\n", num1, num2);
if (scan.hasNext("q")) break;
if (scan.nextInt() == (num1 % num2)) {
score += 20;
done += 1;
correct += 1;
System.out.println("Correct answer,current score :" + score
+ ",performance: "
+ correct + "/" + done);
} else {
done += 1;
System.out.println("Incorrect answer, Current score:" +
score
+ ", performance: " + correct + "/" + done);
}
}
} catch (InputMismatchException e) {
System.out.println("invalid input"); //but this terminate program
}
System.out.println("Finish");
}
代码应该像这样运行:
18 % 12 = ?
6
Correct answer, Current score: 20, performance: 1/1
14 % 16 = ?
a
Invalid input
14 % 16 = ?
14
Correct answer, Current score: 40, performance: 2/2
20 % 4 = ?
q
Finish.
你需要在try-catch
区块内移动while
。此外,当有一个InputMismatchException
你必须读完线(因为你使用Scanner#nextInt
而不是Scanner#nextLine
)并设置一个变量(repeatValue
)到true
。使用此变量,您可以决定是否需要生成新值或使用以前的值。
看到它running here:
public static void main(String[] args) {
int score = 0;
int correct = 0;
int done = 0;
Scanner scan = new Scanner(System.in);
boolean repeatValue = false;
int num1 = 0; // put values outside while in order to re-use them when we need to repeat the same question
int num2 = 0;
while (true) {
try {
// if the user input was incorrect (repeatValue = true), use old the previous values for num1 and num2
num1 = repeatValue ? num1 : (int) (Math.random() * 20);
num2 = repeatValue ? num2 : (int) ((Math.random() * 20) + 1);
System.out.printf("%d %% %d = ?\n", num1, num2);
repeatValue = false; // restore flag state
if (scan.hasNext("q"))
break;
if (scan.nextInt() == (num1 % num2)) {
score += 20;
done += 1;
correct += 1;
System.out.println(
"Correct answer,current score :" + score + ",performance: " + correct + "/" + done);
} else {
done += 1;
System.out.println(
"Incorrect answer, Current score:" + score + ", performance: " + correct + "/" + done);
}
} catch (InputMismatchException e) {
System.out.println("invalid input");
scan.next();
repeatValue = true; // flag set to use the same values as before
}
}
System.out.println("Finish");
}
您的try / catch块位于错误的位置。它必须在你的循环中,以避免使用无效输入打破循环:
public static void main(String[] args) {
int score = 0;
int correct = 0;
int done = 0;
Scanner scan = new Scanner(System.in);
while (true) {
int num1 = (int) (Math.random() * 20);
int num2 = (int) ((Math.random() * 20) + 1);
System.out.printf("%d %% %d = ?\n", num1, num2);
if (scan.hasNext("q")) break;
try {
if (scan.nextInt() == (num1 % num2)) {
score += 20;
done += 1;
correct += 1;
System.out.println(
"Correct answer,current score :" + score + ",performance: " + correct + "/" + done);
} else {
done += 1;
System.out.println(
"Incorrect answer, Current score:"
+ score
+ ", performance: "
+ correct
+ "/"
+ done);
}
} catch (InputMismatchException e) {
done += 1;
scan.nextLine();
System.out.println("invalid input"); // but this terminate program
}
}
scan.close();
System.out.println("Finish");
}
您必须清空catch块中的Scanner以避免无限循环。