简体   繁体   English

无效的输入 while 循环字符串

[英]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.

相关问题 如何使用while循环验证输入字符串 - How to validate input string using while loop input.nextLine()在while循环内有一个字符串 - input.nextLine() with a string inside a while loop 从字符串输入退出While循环 - Exit While loop from String input 使用while循环验证字符串输入 - Validating string input using while loop 为什么我的 do-while 循环在使用 Java 扫描仪输入无效输入后变成无限循环? - Why does my do-while loop become an infinite loop after entering an invalid input with the Java Scanner? 虽然在我的switch语句中循环了while,以验证用户输入,但是尽管有效,但仍继续获得无效输入。 - While loop inside my switch statement to validate user input, but keep getting invalid input although it is valid. Java - 在没有给出无效输入的情况下扫描ArrayList中的整数时,有没有办法打破while循环? - Java - Is there any way to break while loop while scanning integers in ArrayList without giving invalid input? 如何将用户输入用于while循环中的字符串数组? - How do i use user input for a string array that is in a while loop? 尝试验证字符串输入时卡在while循环中 - Stuck in while loop when trying to validate a string input Java-在无限循环中在do中返回多个字符串用户输入 - Java - Return multiple string user input in do while infinite loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM