在 Java 中输入无效后重新提示用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18721884/
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
Re-prompt user after invalid input in Java
提问by user2704743
I'm writing this program in java where I need to re-prompt the user after an invalid input. I came to a solution only to discover that if the user enters another invalid input after the re-prompt then it continues. Can someone please show me a better solution to this? I'll show you what I had anyway:
我正在用 java 编写这个程序,我需要在输入无效后重新提示用户。我找到了一个解决方案,却发现如果用户在重新提示后输入另一个无效输入,则它会继续。有人可以告诉我一个更好的解决方案吗?我会告诉你我有什么:
System.out.println("What is your age?\n");
age = userInput.nextInt();
if((age > 120) || (age < 1)) {//error message
System.out.println("ERROR Please enter a valid age");
System.out.println("");
System.out.println("What is your age?\n");
age = userInput.nextInt();
}//end if
if the user entered an invalid input after they were prompted again, the program would just continue, how can I overcome this?
如果用户在再次提示后输入了无效的输入,程序将继续,我该如何克服?
采纳答案by user2704743
Replace if
with while
.
替换if
为while
。
BAM, problem solved.
BAM,问题解决了。
回答by Matt Bryant
Use a while
loop.
使用while
循环。
while (true) {
System.out.println("What is your age?\n");
age = userInput.nextInt();
if ((age > 120) || (age < 1))
System.out.println("ERROR Please enter a valid age\n");
else
break;
}
回答by BlakeP
You could put it in to a while loop so that it keeps looping until the conditions are met --
您可以将其放入 while 循环中,以便它一直循环直到满足条件-
System.out.println("What is your age?\n");
age = userInput.nextInt();
while((age > 120) || (age < 1)) {//error message
System.out.println("ERROR Please enter a valid age");
System.out.println("");
System.out.println("What is your age?\n");
age = userInput.nextInt();
}//end if
回答by agad
use do-while:
使用 do-while:
boolean valid;
do {
System.out.println("What is your age?\n");
age = userInput.nextInt();
valid = age > 1 && age < 120;
if (!valid) {
System.out.println("ERROR Please enter a valid age");
}
}while (!valid);
回答by Cosmopus
What about this
那这个呢
---->One time check - is your input is empty or just pressed the spacebar
---->一次检查 - 您的输入是空的还是只是按下了空格键
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a string: ");
String input = scnr.nextLine();
boolean isEmpty = input == null || input.trim().length() == 0;
if (isEmpty){
System.out.println("Enter a string again: ");
input = scnr.nextLine();
}
------> Multiple time check- is your input is empty or just pressed the spacebar
------> 多次检查-您的输入是空的还是只是按下了空格键
Scanner scnr = new Scanner(System.in);
do {
System.out.println("Enter a string: ");
input = scnr.nextLine();
}
while (input == null || input.trim().length() == 0);
Important: Don't forget that input should be static string in this case.
重要提示:不要忘记在这种情况下输入应该是静态字符串。
static String input="";
回答by Aadil Chan
// Using do-while loop, this problem can be tackled.
// 使用 do-while 循环,可以解决这个问题。
do {
System.out.println("Enter a pin: ");
pin = sc.nextInt();
} while (pin != 12345);
System.out.println("Welcome to program");