Java:Do-While 循环中的 Try-Catch 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22004921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Try-Catch statement within a Do-While loop
提问by
I am trying to execute a bit of code that scans for a user input value. This action is contained in a custom method I wrote, named getTriangleDim(); the method reads in the users int value, makes sure it is within a certain range, and then returns the int value that was entered. The method works great and I have no issues with it.
我正在尝试执行一些扫描用户输入值的代码。此操作包含在我编写的自定义方法中,名为 getTriangleDim(); 该方法读取用户的 int 值,确保它在一定范围内,然后返回输入的 int 值。该方法效果很好,我对此没有任何问题。
The problem arises when I enter a non-int value for my getTriangleDim() method. It gives me an InputMismatchException error. I have written a try-catch statement into a do-while loop to attempt to fix this issue. But this is the first time I have ever used a try-catch statement, and I am apparently missing something.
当我为 getTriangleDim() 方法输入一个非整数值时,问题就出现了。它给了我一个 InputMismatchException 错误。我已将 try-catch 语句写入 do-while 循环以尝试解决此问题。但这是我第一次使用 try-catch 语句,显然我遗漏了一些东西。
Here is the code for the try-catch statement nested within the do-while loop:
下面是嵌套在 do-while 循环中的 try-catch 语句的代码:
//loop to scan for triangle dimension
boolean bError = true;
int triangle;
do{
try {
triangle = getTriangleDim();
bError=false;
}
catch (Exception e){
System.out.println("You did not enter an integer, please enter an integer value");
triangle = getTriangleDim();
}
}while (bError);
if i test it by entering a char value in place of the int, it actually catches the error once, and then prints my "you did not....." statement. But if I re-enter another non-int number, I get a runtime error again that says.......you guessed it........ InputMismatchException error.
如果我通过输入一个字符值代替 int 来测试它,它实际上会捕获一次错误,然后打印我的“你没有......”语句。但是如果我重新输入另一个非整数数字,我会再次收到一个运行时错误,说......你猜对了...... InputMismatchException 错误。
The code for my method is here:
我的方法的代码在这里:
//method for scanning triangle dimensions from keyboard
public static int getTriangleDim(){
int triangle = 0;
Scanner keyboard = new Scanner(System.in);
do{
System.out.print("Enter a non-zero integer length (+/-1 - +/-16): ");
triangle = keyboard.nextInt();
if((!(triangle <= 16 && triangle >= 1))&&(!(triangle >= -16 && triangle <= -1)))
System.out.println("Inpute value outside of range");
}while((!(triangle <= 16 && triangle >= 1))&&(!(triangle >= -16 && triangle <= -1)));
return triangle;
}
I need the Do-While loop to continue, but I keep getting these errors.
我需要 Do-While 循环才能继续,但我不断收到这些错误。
采纳答案by Henry Keiter
There's no need to ask for input in the catch
block. You're already in a loop, so you can catch the exception, tell the user to give you valid input, and then you don't have to do anything else--you'll loop back around to the beginning.
无需在catch
块中要求输入。你已经在一个循环中,所以你可以捕捉异常,告诉用户给你有效的输入,然后你不必做任何其他事情——你将循环回到开头。
do{
try {
triangle = getTriangleDim();
bError=false;
} catch (Exception e){
System.out.println("You did not enter an integer, please enter an integer value");
// Don't do anything else in here: we will loop back to the beginning again and get new input!
}
}while (bError);
As a side note (and as you've noticed), your code won't currently compile if you try to use triangle
outside of that try
block, because "triangle might not have been initialized"
. This is due to the fact that the compiler cannot determine at compile-time what your program will do at run-time: that is, the compiler cannot see that triangle
will always be initialized within that loop. So your variable declaration ought to also set triangle
to some default value. 0
is the normal default for int
, but use whatever makes sense in your program (it looks like 0
is fine, based on your getTriangleDim
code).
作为旁注(正如您已经注意到的),如果您尝试triangle
在该try
块之外使用,您的代码当前不会编译,因为"triangle might not have been initialized"
. 这是因为编译器无法在编译时确定您的程序将在运行时做什么:也就是说,编译器看不到它triangle
总是在该循环内初始化。所以你的变量声明也应该设置triangle
为一些默认值。0
是 的正常默认值int
,但请使用您程序中有意义的任何内容(0
根据您的getTriangleDim
代码,它看起来很好)。
int triangle = 0;
do { // etc.
This way you can "promise" the compiler that triangle
will have a value by the time you leave the loop, and you'll be able to use it elsewhere.
通过这种方式,您可以“承诺”在triangle
您离开循环时将具有值的编译器,并且您将能够在其他地方使用它。