Java 为什么try-catch最终没有重新提示用户?
我对Java和所有编程都比较陌生。我正在为学校写一个简单的猜测数字程序。用户输入数字1-10,并将其与随机数字1-10进行比较。req规范规定,应检查所有无效输入,如果发现无效输入,应重新提示用户。为此,我正在使用try-catch-finally。如果int guess确实不是整数,我的catch块将查找该异常,但它不会重新提示用户,而是保留初始值0。我只是在用户输入之前初始化了猜测,因为终端反复对我大喊大叫,直到我这么做。我也不确定为什么必须在try块之前初始化变量 我到处寻找,但我发现的每个解决方案的代码在结构上都与我的几乎相同。我猜这一定是我不熟悉/其他人忽略了提及的东西,因为它太简单了。顺便说一下,这是我第一次使用try-catch-finallyJava 为什么try-catch最终没有重新提示用户?,java,Java,我对Java和所有编程都比较陌生。我正在为学校写一个简单的猜测数字程序。用户输入数字1-10,并将其与随机数字1-10进行比较。req规范规定,应检查所有无效输入,如果发现无效输入,应重新提示用户。为此,我正在使用try-catch-finally。如果int guess确实不是整数,我的catch块将查找该异常,但它不会重新提示用户,而是保留初始值0。我只是在用户输入之前初始化了猜测,因为终端反复对我大喊大叫,直到我这么做。我也不确定为什么必须在try块之前初始化变量 我到处寻找,但我发现的每
int guess = 0;
int number = (int)((Math.random()*10) + 1);
System.out.print("Please guess a number between 1 and 10, inclusive: ");
//open try
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
guess = Integer.parseInt(br.readLine());
//monitor input
while(guess < 1 || guess > 10) {//open while
System.out.print("You seem to have entered something wrong. Please only");
System.out.println(" enter ");
System.out.print("an integer number between 1 and 10, 1 and 10 included: ");
guess = Integer.parseInt(br.readLine());
}//close while
//close try
} catch(NumberFormatException e) {//open catch
System.out.print("You seem to have entered something wrong. Please only");
System.out.println(" enter ");
System.out.print("an integer number between 1 and 10, 1 and 10 included: ");
//close catch
} finally {//open finally
//user feedback
if(number == guess){
System.out.println("You got it! You guessed the number correctly. It is "
+ number + ".");
} else {
System.out.println("I'm sorry! That is not the number I was thinking of.");
System.out.println("You guessed " + guess + ", but the number is "
+ number + ".");
}
}//close finally
从
parseInt
引发的异常会导致循环中断。您可能希望您的循环包含try-catch-finally子句,而不是try-catch-finally子句。不管异常与否,finally部分都会发生。
您应该将finally部分的代码放在try部分。您应该首先组织代码,正如其他人所说,您应该在try中执行代码并处理catch上的异常,如果您要在try块中执行,那么最后是多余的。举个例子,你还有另外一个问题,他们比我解释得更好。希望有帮助。 [连结]
另外,当你输入一个数字时,NumberformatException也会起作用,但我认为你错过了IOException。如果你不输入一个数值,你会被异常抛出循环。你应该在while循环中捕获异常。非常感谢你的输入。我一起删除了finally块,因为这对我来说是不必要的。我将不得不仔细阅读异常类型。。。感谢您的输入,我添加了第二个catch块来捕获IOExceptions。这个词“break”让我思考,我将它全部放入while循环,就像您所说的,并在catch块的末尾添加了一些continue语句。请参考编辑的原始帖子,如果您知道为什么需要continue语句,以及它如何真正影响循环流,请告诉我对不起,我刚刚做了。现在,它在原始帖子的末尾被编辑。我是这个平台的新手,所以我不知道添加第二块代码是否是非正式的/违反规则的。我不认为有必要使用
continue
语句。尝试调试程序以查看差异。如果你没有成功,那么也许你应该把它作为一个单独的问题发布。我只是简单地删除了它们,所有这些都起了作用。。。也许我之前错过了一个支架什么的。非常感谢你的帮助
//init vars needed
int guess;
int number = (int)((Math.random()*10) + 1);
boolean done = false;
System.out.print("Please guess a number between 1 and 10, inclusive: ");
while(!done) {//open while
try {//open try
guess = Integer.parseInt(br.readLine());
while(guess < 1 || guess > 10) {
System.out.print("You seem to have entered something wrong. Please only");
System.out.println(" enter ");
System.out.print("an integer number between 1 and 10, 1 and 10 included: ");
guess = Integer.parseInt(br.readLine());
}
if(number == guess) {
System.out.println("You got it! You guessed the number correctly. It is "
+ number + ".");
} else if(number != guess) {
System.out.println("I'm sorry! That is not the number I was thinking of.");
System.out.println("You guessed " + guess + ", but the number is "
+ number + ".");
}
done = true;
} //close try
catch(NumberFormatException e) {//open catch
System.out.print("You seem to have entered something wrong. Please only");
System.out.println(" enter ");
System.out.print("an integer number between 1 and 10, 1 and 10 included: ");
continue;
}//close catch
catch(IOException e) {//open catch
System.out.print("You seem to have entered something wrong. Please only");
System.out.println(" enter ");
System.out.print("an integer number between 1 and 10, 1 and 10 included: ");
continue;
}//close catch
}//close while
try {
System.out.println("Im the try block");
}
finally {
System.out.println("Im the finally block");
}
Output:
Im the finally block
Im the try block