英文:
How to prevent a user from entering a invalid answer
问题
我已经尝试阻止用户输入除了"yes"或"no"之外的任何内容,但当他们这样做时,它会一直显示错误,请再试一次。
public static void main(String[] args) {
System.out.println("你想要添加一个收据吗?是或否");
Scanner target = new Scanner(System.in);
String TempAnswer;
TempAnswer = target.nextLine();
Boolean flag = true;
while (flag == true) {
if ("是".equalsIgnoreCase(TempAnswer)) {
System.out.println("是");
flag = false;
} else if ("否".equalsIgnoreCase(TempAnswer)) {
System.out.println("否");
flag = false;
} else {
System.out.println("错误,请重新输入!!");
}
}
}
英文:
I have tried to stop the user from entering anything besides yes or no however when they do it just keeps on saying error try again forever.
public static void main(String[] args) {
System.out.println
("Would you like to add a Reciept. Yes or No");
Scanner target = new Scanner(System.in);
String TempAnswer;
TempAnswer = target.nextLine();
Boolean flag = true;
while (flag == true) {
if ("yes".equalsIgnoreCase(TempAnswer)) {
System.out.println("Yes");
flag = false;
} else if ("no".equalsIgnoreCase(TempAnswer)) {
System.out.println("No");
flag = false;
} else {
System.out.println("Error Enter Again!!");
}
}
}
答案1
得分: 1
你在 while 循环中从未将输入值重新分配给你的 TempAnswer
变量,导致循环无限运行,因为你从未给予布尔值根据用户输入进行更改的机会。
一些附注:
- 使用 Java 命名约定,用 camelCase 定义你的成员变量标识符,而不是 PascalCase;
- 不要引入多余的代码。使用
while(true)
替代定义布尔变量flag
并将其用作条件;相应地,使用break;
替代将flag
设置为 false。
最终,我会将你的代码重写如下:
public static void main(String[] args) {
System.out.println("Would you like to add a Reciept. Yes or No");
Scanner target = new Scanner(System.in);
String tempAnswer = "";
while (true) {
if ("yes".equalsIgnoreCase(tempAnswer)) {
System.out.println("Yes");
break;
} else if ("no".equalsIgnoreCase(tempAnswer)) {
System.out.println("No");
break;
} else {
System.out.println("Error Enter Again!!");
tempAnswer = target.nextLine();
}
}
}
英文:
You're never re-assigning the input value to your TempAnswer
variable in the while loop and loop runs infinitely, as you never give the chance to the boolean value to be changed, based on user's input.
Few side-notes:
- Use Java Naming Conventions and define your member variable identifiers with camelCase, not with PascalCase;
- Do not introduce redundant code. Use
while(true)
instead of defining boolean variableflag
and using it as the condition; correspondingly, usebreak;
instead of settingflag
to false.
Eventually, I would have rewritten your code as follows:
public static void main(String[] args) {
System.out.println("Would you like to add a Reciept. Yes or No");
Scanner target = new Scanner(System.in);
String tempAnswer = "";
while (true) {
if ("yes".equalsIgnoreCase(tempAnswer)) {
System.out.println("Yes");
break;
} else if ("no".equalsIgnoreCase(tempAnswer)) {
System.out.println("No");
break;
} else {
System.out.println("Error Enter Again!!");
tempAnswer = target.nextLine();
}
}
}
答案2
得分: 1
public static void main(String[] args) {
System.out.println("Would you like to add a Receipt. Yes or No");
Scanner target = new Scanner(System.in);
while (true) {
String tempAnswer = target.nextLine();
if ("yes".equalsIgnoreCase(tempAnswer)) {
System.out.println("Yes");
break;
}
if ("no".equalsIgnoreCase(tempAnswer)) {
System.out.println("No");
break;
}
System.out.println("Error Enter Again!!");
}
}
英文:
Get rid of the superfluous calls and make sure that you read the answer in the loop. If you don't do that, you'll indefinitely loop because you're not changing the loop-condition.
Use break to break the loop instead of using flags for such small scope.
Also, don't use Boolean
, use boolean
instead. You will probably never have to use Boolean
, so don't use it.
public static void main(String[] args) {
System.out.println("Would you like to add a Reciept. Yes or No");
Scanner target = new Scanner(System.in);
while (true) {
String tempAnswer = target.nextLine();
if ("yes".equalsIgnoreCase(tempAnswer)) {
System.out.println("Yes");
break;
}
if ("no".equalsIgnoreCase(tempAnswer)) {
System.out.println("No");
break;
}
System.out.println("Error Enter Again!!");
}
}
答案3
得分: -1
尝试在输入正确答案后终止循环
public static void main(String[] args) {
System.out.println("Would you like to add a Receipt. Yes or No");
Scanner target = new Scanner(System.in);
String TempAnswer;
TempAnswer = target.nextLine();
while (true) {
if ("yes".equalsIgnoreCase(TempAnswer)) {
System.out.println("Yes");
break;
} else if ("no".equalsIgnoreCase(TempAnswer)) {
System.out.println("No");
break;
} else {
System.out.println("Error Enter Again!!");
}
}
}
英文:
Try to break the loop when the correct answer is typed
public static void main(String[] args) {
System.out.println
("Would you like to add a Reciept. Yes or No");
Scanner target = new Scanner(System.in);
String TempAnswer;
TempAnswer = target.nextLine();
while (True) {
if ("yes".equalsIgnoreCase(TempAnswer)) {
System.out.println("Yes");
break;
} else if ("no".equalsIgnoreCase(TempAnswer)) {
System.out.println("No");
break;
} else {
System.out.println("Error Enter Again!!");
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论