java 异常后继续while循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25101575/
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
Continue a while loop after exception
提问by dorothy
i have this piece of code. I wanted to return to the beginning of loop and ask for user input again. However, it always loops without stopping to ask for input. What is wrong with my code? thanks
我有这段代码。我想返回到循环的开头并再次要求用户输入。然而,它总是循环而不停止请求输入。我的代码有什么问题?谢谢
while(true){
...
try {
int choice = input.nextInt(); <<---=- this should stop and ask for input, but it always loops without stopping.
} catch (InputMismatchException e){
<< I want to return to the beginning of loop here >>
}
}
回答by David K
From http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28int%29:
从http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28int%29:
"If the translation is successful, the scanner advances past the input that matched."
“如果翻译成功,扫描仪就会越过匹配的输入。”
Ah, but what if the translation is notsuccessful? In that case, the scanner does not advance past any input. The bad input data remains as the next thing to be scanned, so the next iteration of the loop will fail just like the previous one--the loop will keep trying to read the same bad input over and over.
啊,但是如果翻译是什么不是成功的?在这种情况下,扫描仪不会通过任何输入。错误的输入数据仍然作为下一个要扫描的内容,因此循环的下一次迭代将像前一次一样失败——循环将不断尝试一遍又一遍地读取相同的错误输入。
To prevent an infinite loop, you have to advance past the bad data so that you can get to something the scanner can read as an integer. The code snippet below does this by calling input.next():
为了防止无限循环,您必须跳过坏数据,以便获得扫描仪可以读取的整数。下面的代码片段通过调用 input.next() 来做到这一点:
Scanner input = new Scanner(System.in);
while(true){
try {
int choice = input.nextInt();
System.out.println("Input was " + choice);
} catch (InputMismatchException e){
String bad_input = input.next();
System.out.println("Bad input: " + bad_input);
continue;
}
}
回答by Elliott Frisch
You haven't posted anything asking for input,
你还没有发布任何要求输入的内容,
Scanner input = new Scanner(System.in);
int choice;
while (true) {
System.out.println("Please enter an int: ");
if (input.hasNextInt()) { // <-- Check if there is an int.
choice = input.nextInt();
break;
} else {
if (!input.hasNext()) { // <-- Check if there is input.
System.err.println("No more input");
System.exit(1);
}
// What ever is in the buffer isn't an int, print the error.
System.out.printf("%s is not an int%n", input.next());
}
}
// Display the choice.
System.out.printf("choice = %d%n", choice);
回答by S.Yavari
This works fine:
这工作正常:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
while(true){
try {
choice = input.nextInt();
System.out.println("Your choice: " + choice);
} catch (InputMismatchException e){
e.printStackTrace();
}
}
}
}
回答by dgl
Try doing do while loop.
尝试做 while 循环。
do
{
try
{
//get user input
done = true;
}
catch (InputMismatchException e)
{
System.out.println("The number entered needs to be a int");
}
} while (!done);
回答by ednomx
This should throw and catch the exception and the continue
command should send you back to your while loop
. you need either a continue or a flag
to tell your while when it stops being true
.
这应该抛出并捕获异常,并且continue
命令应该将您发送回您的while loop
. 你需要一个 continue 或 aflag
来告诉你它什么时候停止true
。
while(true)
{
try
{
int choice = input.nextInt();
throw new InputMismatchException();
}
catch (InputMismatchException e)
{
continue;
}
}
回答by Kuldeep Verma
Put a line separator in your catch
block.
在catch
块中放置一个行分隔符。
Scanner input = new Scanner(System.in);
while(true)
{
try
{
int choice = input.nextInt();
} catch (InputMismatchException e)
{
input.next(); // Line separator
}
}