[英]invalid input while loop string
I'm writing a program for a museum ticket system where the user enters the exhibit they would like to visit however when there is invalid input the program skips the if statement and continues the rest of the program.我正在为博物馆门票系统编写一个程序,用户可以在其中输入他们想要参观的展览,但是当输入无效时,程序会跳过 if 语句并继续程序的其余部分。
What im trying do here is make the program loop back to the start of the if statement when input data is invalid.我在这里尝试做的是在输入数据无效时让程序循环回到 if 语句的开头。
while(invalidinput){
if (userinput.equalsIgnoreCase("Roman")) {
System.out.println("There are " +availabletic.getAvailabletickets1()+ " tickets available for the "+exhibit.getExhibit1());
System.out.println("");
} else if (userinput.equalsIgnoreCase("Warhol Art")){
System.out.println("There are " +availabletic.getAvailabletickets2()+ " tickets available for the "+exhibit.getExhibit2());
System.out.println("");
} else if (userinput.equalsIgnoreCase("World War 2")) {
System.out.println("There are " +availabletic.getAvailabletickets3()+ " tickets available for the "+exhibit.getExhibit3());
System.out.println("");}
} else if (!userinput.equalsIgnoreCase("Roman")
&& !userinput.equalsIgnoreCase("Warhol Art")
&& !userinput.equalsIgnoreCase("World War 2"))
invalidinput = false; // We signal that we may now leave the loop
{
System.out.println("Please enter a valid exhibition name");
}
}
This condition:这个条件:
else if (!userinput.equalsIgnoreCase("Roman")
&& !userinput.equalsIgnoreCase("Warhol Art")
&& !userinput.equalsIgnoreCase("World War 2"))
must be replaced by:必须替换为:
else if (!userinput.equalsIgnoreCase("Roman")
|| !userinput.equalsIgnoreCase("Warhol Art")
|| !userinput.equalsIgnoreCase("World War 2"))
You don't need a catch-all else-if.你不需要一个包罗万象的如果。
if (userinput.equalsIgnoreCase("Roman"))
else if (userinput.equalsIgnoreCase("Warhol Art"))
else if (userinput.equalsIgnoreCase("World War 2"))
else // It's none of the others.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.