简体   繁体   English

为什么即使不满足循环内设置的条件,我的循环错误消息仍会打印出来

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

相关问题 即使不再满足条件,while循环也会继续 - while loop continues even if the condition is not satisfied anymore 为什么我的循环总是通过,即使条件并不总是满足? - Why does my loop always go through even though the conditions aren't always met? 即使我的 for 循环中有一个 return 语句,为什么我会因为没有 return 语句而收到错误消息? - Why do I get an error for not having a return statement even though I have one in my for loop? 即使在多线程popgram中满足条件后,while循环也不会以java结尾 - while loop not ending in java even after condition is satisfied in multithreaded popgram (Java)如果从未满足过for循环中if语句的条件,我该如何打印一条消息,指出“没有值满足此条件”? - (Java) If the condition for an if statement inside a for loop is never met, how do I print out a message saying “no values meet this criteria”? 为什么 for readelements() 来回循环不打印出来? - Why does for readelements() fro loop not print out? 为什么我的方法说它必须返回一个类型字符串并给我一个错误,即使我的方法确实返回一个类型字符串 - Why does my method say it must return a type string and gives me an error even though my method does return a type string 为什么不满足此条件? - Why is this condition not satisfied? 为什么这个条件从不满足? - Why is this condition never satisfied? 为什么即使我声明了 rand,它也会给我一个错误? - Why does it give me an error even though I declared rand?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM