Java 在 While 循环中尝试捕获
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24857070/
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
Try-Catch inside While Loop
提问by Rhendz
The code below asks the user how many racers he/she would like.
下面的代码询问用户他/她想要多少赛车手。
while (true) { // loops forever until break
try { // checks code for exceptions
System.out.println("How many racers should" + " participate in the race?");
amountRacers = in.nextInt();
break; // if no exceptions breaks out of loop
}
catch (InputMismatchException e) { // if an exception appears prints message below
System.err.println("Please enter a number! " + e.getMessage());
continue; // continues to loop if exception is found
}
}
If a number is entered at amoutnRacers = in.nextInt();
the code breaks out of the loop and the rest of the program runs fine; however, when I enter something such as "awredsf" it should catch that exception, which it does. Instead of prompting the user again it loops continuously, which to me does not make sense.
如果在amoutnRacers = in.nextInt();
代码处输入了一个数字,则该代码跳出循环并且程序的其余部分运行良好;但是,当我输入诸如“awredsf”之类的内容时,它应该捕获该异常,它确实做到了。它不是再次提示用户,而是连续循环,这对我来说没有意义。
The program prints like this when looping continuously:
程序在连续循环时打印如下:
How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race?Please enter a number! null Please enter a number! null Please enter a number! null Please enter a number! null Please enter a number! null Please enter a number! null Please enter a number! null ...
应该有多少赛车手参加比赛?应该有多少赛车手参加比赛?应该有多少赛车手参加比赛?应该有多少赛车手参加比赛?应该有多少赛车手参加比赛?应该有多少赛车手参加比赛?应有多少车手参加比赛?请输入数字!null 请输入数字!null 请输入数字!null 请输入数字!null 请输入数字!null 请输入数字!null 请输入数字!空值 ...
I do not understand what is going on amountRacers = in.nextInt();
so why is the user not able to enter a number?
我不明白发生了什么amountRacers = in.nextInt();
,为什么用户无法输入数字?
采纳答案by Juned Ahsan
Just add input.next()
once you catch InputMismatchException.
input.next()
捕获 InputMismatchException 后只需添加 。
catch (InputMismatchException e) { //if an exception appears prints message below
System.err.println("Please enter a number! " + e.getMessage());
input.next(); // clear scanner wrong input
continue; // continues to loop if exception is found
}
You need to clear the wrong input, which scanner automatically does not.
您需要清除错误的输入,而扫描仪会自动不清除。
回答by ConsciousCoder
You may need to create a Scanner class for getting standard input streamed from the keyboard. You should have a statement somewhere in your code that creates an instance of a Scanner class like: Scanner in = new Scanner(System.in);
so the " in " variable in your statement: amountRacers = in.nextInt(); waits and scans for entered input from the keyboard and stores it.
您可能需要创建一个 Scanner 类来获取从键盘流式传输的标准输入。您应该在代码中的某处创建一个语句,该语句创建 Scanner 类的实例,例如:Scanner in = new Scanner(System.in);
所以语句中的“in”变量:amountRacers = in.nextInt(); 等待并扫描从键盘输入的输入并存储它。
回答by Wojciech Pilzak
Today i solved this problem :-) This is my code. I think that i help
今天我解决了这个问题:-) 这是我的代码。我想我有帮助
public int choice () throws Exception{
Scanner read = new Scanner(System.in));
System.out.println("Choose the option from the upper list");
int auxiliaryChoiceMenu = 5;
int auxiliaryVariable = -1;
boolean auxiliaryBoolean = false;
while (!auxiliaryBoolean) {
try {
auxiliaryVariable = read.nextInt();
read.nextLine();
} catch (Exception e) {
System.out.println("incorrect data, try again"+e);
read.nextLine();
continue;
}
if (auxiliaryVariable<0 || auxiliaryVariable>auxiliaryChoiceMenu){
System.out.println("incorrect data, try again");
} else {
auxiliaryBoolean = true;
}
choiceMenu = auxiliaryVariable;
}
return choiceMenu;
//choicemenu is a external variable
}