[英]Why does my looped error message print out even though the condition set inside the loop is not satisfied
Here's a snip of my code: 这是我的代码片段:
while (true){
System.out.println("---Welcome to the Shape Machine---");
System.out.println("Available options:");
System.out.println("Circles");
System.out.println("Rectangles");
System.out.println("Triangles");
System.out.println("Exit");
//asks for selection
String option = console.next();
while (!option.equals("Cirlces") && !option.equals("Rectangles") && !option.equals("Triangles") && !option.equals("Exit")){
System.out.println("#ERROR Invalid option. Please try again.");
break;
}
switch (option) {
case "Circles": {
I have a menu set up and when the user inputs anything that isnt one of the options it's supposed to print out the error message and brings the user back into the menu. 我已经设置了一个菜单,当用户输入的内容不是该选项之一时,它应该打印出错误消息并将用户带回菜单。 That works as intended, but if I put in a correct input the error message still prints out, but the switch statement runs as if there is no error and does the necessary calculations. 那可以按预期工作,但是如果我输入正确,错误消息仍然会打印出来,但是switch语句会像没有错误一样运行并进行必要的计算。 I've tried using a while true loop within an if else statement and I still had the same problem. 我曾尝试在if else语句中使用while true循环,但仍然遇到相同的问题。 I also tried using an OR operator instead of an AND operator along with using a != instead of the !().equals method. 我还尝试使用OR运算符代替AND运算符,以及使用!=代替!()。equals方法。 I have no idea what to do to fix it. 我不知道该如何解决。 Any help would be very much appreciated. 任何帮助将不胜感激。
I'm gonna go on a wild guess here and try to figure out what you were trying to accomplish. 我将在这里进行一个疯狂的猜测,并尝试弄清楚您要实现的目标。
Try this: 尝试这个:
while (true){
System.out.println("---Welcome to the Shape Machine---");
System.out.println("Available options:");
System.out.println("Circles");
System.out.println("Rectangles");
System.out.println("Triangles");
System.out.println("Exit");
//asks for selection
String option = console.next();
switch (option) {
case "Circles":
//do something
break;
case "Rectangles":
break;
case "Triangles":
break;
case "Exit":
break;
default:
System.err.println("#ERROR Invalid option. Please try again.");
}
//now you can either put a flag or change the code to a DO..While
//depending on if you want to re-execute after each option..
}
If you want an if statement, you're gonna wanna do (to follow your version): 如果您想要一个if语句,您将想要(遵循您的版本):
if (!option.equals("Cirlces") && !option.equals("Rectangles") && !option.equals("Triangles") && !option.equals("Exit")){
//print the error, then continue
}
or, easier to read 或者,更容易阅读
if( ! ( (option.equals("Circles") || option.equals("Rectangles") || option.equals("Triangles") || option.equals("Exit") ) ){
//print the error, then continue
}
Also please make sure that you're reading the right value, try printing it out and check. 另外,请确保您正在读取正确的值,尝试将其打印出来并检查。
If this doesn't work, there must be an error in the code you didn't provide, in that case please post a MCVE . 如果这不起作用,那么您未提供的代码中肯定存在错误,在这种情况下,请发布MCVE 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.