Java 绕着一个试球转
在下面的代码中,我试图允许程序捕捉来自用户的无效输入的异常,但仍然允许程序在捕捉到异常后循环回方法的开头。然而,在我的示例中,一旦出现异常,程序就会终止。我怎样才能纠正这个问题?提前多谢Java 绕着一个试球转,java,loops,exception,try-catch,Java,Loops,Exception,Try Catch,在下面的代码中,我试图允许程序捕捉来自用户的无效输入的异常,但仍然允许程序在捕捉到异常后循环回方法的开头。然而,在我的示例中,一旦出现异常,程序就会终止。我怎样才能纠正这个问题?提前多谢 public static void add() { // Setting up random Random random = new Random(); // Declaring Integers int num1; int num2; int result;
public static void add() {
// Setting up random
Random random = new Random();
// Declaring Integers
int num1;
int num2;
int result;
int input;
input = 0;
// Declaring boolean for userAnswer (Defaulted to false)
boolean correctAnswer = false;
do {
// Create two random numbers between 1 and 100
num1 = random.nextInt(100);
num1++;
num2 = random.nextInt(100);
num2++;
// Displaying numbers for user and getting user input for answer
System.out.println("Adding numbers...");
System.out.printf("What is: %d + %d? Please enter answer below", num1, num2);
result = num1 + num2;
do {
try {
input = scanner.nextInt();
} catch (Exception ex) {
// Print error message
System.out.println("Sorry, invalid number entered for addition");
// flush scanner
scanner.next();
correctAnswer=false;
}
} while (correctAnswer);
// Line break for code clarity
System.out.println();
// if else statement to determine if answer is correct
if (result == input) {
System.out.println("Well done, you guessed corectly!");
correctAnswer = true;
} else {
System.out.println("Sorry incorrect, please guess again");
}
} while (!correctAnswer);
}// End of add
我不太清楚异常部分,但您是否考虑过只使用
if
语句
Scanner
有一个方法“hasnetint
”,可用于检查输入是否为na int。例如:
Scanner scan = new Scanner(System.in);
int i=0;
boolean correctAnswer = false;
while(correctAnswer == false){
if(scan.hasNextInt()){
i = scan.nextInt(); correctAnswer = true;
}else{ System.out.println("Invalid entry");
correctAnswer = false;
scan.next();
}
System.out.println(i);
}
很抱歉,它实际上并没有直接回答您的问题,但我想您可能也想知道这种可能的方式 您可以使用hasNextInt()方法,如果令牌是一个数字,该方法将返回true,而不是抛出异常。
但是,如果您想完全使用try catch块,则必须删除scanner.next()指令,因为当缓冲区中没有可用的内容时,它会抛出一个NosTouchElementException我认为我给出的解决方案可以改进,但这是修复代码的简单修改:(只需添加新的条件变量,以检查是否需要进一步的输入/ans尝试) 希望能有帮助-MAK
如果您想要以下内容: 1) 询问用户x+y是多少
2) 让用户回答
3) 如果答案无效(例如用户键入“www”),则让用户再次键入问题1的答案) 然后,应将内部do while循环替换为以下内容:
boolean validInput = true;
do {
try {
input = scanner.nextInt();
} catch (Exception ex) {
// Print error message
System.out.println("Sorry, invalid number entered for addition. Please enter your answer again.");
// flush scanner
scanner.next();
validInput = false;
}
} while (!validInput);
你确定这个异常是在try/catch中抛出的吗?正如@marsze所说,这个错误可能是在
try之外发生的。。。catch…
在这种情况下,它不会被捕获并将终止(除非在别处捕获)。如果您已经确定它在这段代码中,我猜,scanner.next()会触发第二个异常因为它在catch语句中,所以它失败了。你经历了什么样的例外?你的内心似乎从未爆发过。correctAnswer何时设置为true
?以防万一,若有人并没有注意到,若您输入的不是int作为答案,将引发异常:)
boolean validInput = true;
do {
try {
input = scanner.nextInt();
} catch (Exception ex) {
// Print error message
System.out.println("Sorry, invalid number entered for addition. Please enter your answer again.");
// flush scanner
scanner.next();
validInput = false;
}
} while (!validInput);