简体   繁体   中英

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. I've tried using a while true loop within an if else statement and I still had the same problem. I also tried using an OR operator instead of an AND operator along with using a != instead of the !().equals method. 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 (!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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM