我正在为博物馆门票系统编写程序,用户可以输入想要参观的展览品,但是当输入无效时,程序将跳过if语句,并继续执行程序的其余部分。
我在这里试图做的是,当输入数据无效时,使程序循环回到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");
}
}
如果不需要,您不需要万能的。
if (userinput.equalsIgnoreCase("Roman"))
else if (userinput.equalsIgnoreCase("Warhol Art"))
else if (userinput.equalsIgnoreCase("World War 2"))
else // It's none of the others.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句