简体   繁体   English

Java Do While Statement有两个条件

[英]Java Do While Statement with two conditions

I'm trying to learn java but I'm stuck trying to do a single program which concerns Do While Statement with two conditions. 我正在尝试学习java,但我一直试图做一个关于Do While Statement的单个程序,有两个条件。 Specifically, I want a method to run until the user write "yes" or "no". 具体来说,我想要一个方法运行,直到用户写“是”或“否”。 Well, down there is my thing, what is wrong with it? 那么,有我的东西,它有什么问题?

    String answerString;
    Scanner user_input = new Scanner(System.in);
    System.out.println("Do you want a cookie? ");

    do{
    answerString = user_input.next();
    if(answerString.equalsIgnoreCase("yes")){
        System.out.println("You want a cookie.");
    }else if(answerString.equalsIgnoreCase("no")){
        System.out.println("You don't want a cookie.");
    }else{
        System.out.println("Answer by saying 'yes' or 'no'");
    }while(user_input == 'yes' || user_input == 'no');
    }
}}

I'd do something similar to Tim's answer. 我会做一些类似蒂姆的回答。 But to do things the way you were trying to do them, you have a lot of problems that need to be fixed: 但是,按照你试图做的方式做事,你有很多需要修复的问题:

(1) String literals in Java are surrounded by double quote marks, not single quote marks. (1)Java中的字符串文字由双引号括起,而不是单引号。

(2) user_input is a Scanner . (2) user_input是一个Scanner You can't compare a scanner to a string. 您无法将扫描仪与字符串进行比较。 You can only compare a String to another String . 您只能将String与另一个String进行比较。 So you should be using answerString in your comparison, not user_input . 所以你应该在比较中使用answerString ,而不是user_input

(3) Never use == to compare strings. (3)永远不要使用==来比较字符串。 StackOverflow has 953,235 Java questions, and approximately 826,102 of those involve someone trying to use == to compare strings. StackOverflow有953,235个Java问题,其中大约826,102个涉及有人试图使用==来比较字符串。 (OK, that's a slight exaggeration.) Use the equals method: string1.equals(string2) . (好吧,这有点夸张。)使用equals方法: string1.equals(string2)

(4) When you write a do-while loop, the syntax is do , followed by { , followed by the code in the loop, followed by } , followed by while(condition); (4)编写do-while循环时,语法为do ,后跟{ ,后跟循环中的代码,后跟} ,然后是while(condition); . It looks like you put the last } in the wrong place. 它看起来像你把最后}在错误的地方。 The } just before the while belongs to the else , so that doesn't count; }在之前while属于else ,这样不计; you need another } before while , not after it. 你需要另一个}之前, while不是之后。

(5) I think you were trying to write a loop that keeps going if the input isn't yes or no . (5)我觉得你试图写一个循环,不断去,如果输入的是不yesno Instead, you did the opposite: you wrote a loop that keeps going as long as the input is yes or no . 相反,你却反其道而行之:你写了一个循环,只要输入不断去yesno Your while condition should look something like while条件应该是这个样子

while (!(answerString.equals("yes") || answerString.equals("no")));

[Actually, it should be equalsIgnoreCase to be consistent with the rest of the code.] ! [实际上,应该将equalsIgnoreCase与其余代码保持一致。] ! means "not" here, and note that I had to put the whole expression in parentheses after the ! 在这里意味着“不”,并注意到我必须把整个表达式放在括号之后! , otherwise the ! ,否则! would have applied only to the first part of the expression. 只会应用于表达式的第一部分。 If you're trying to write a loop that does "Loop until blah-blah-blah", you have to write it as "Loop while ! (blah-blah-blah)". 如果你正在尝试编写一个“循环直到等等等”的循环,你必须把它写成“Loop while ! (blah-blah-blah)”。

I might opt for a do loop which will continue to take in command line user input until he enters a "yes" or "no" answer, at which point the loop breaks. 我可能会选择一个do循环,它将继续接受命令行用户输入,直到他输入“是”或“否”答案,此时循环中断。

do {
    answerString = user_input.next();

    if ("yes".equalsIgnoreCase(answerString)) {
        System.out.println("You want a cookie.");
        break;
    } else if ("no".equalsIgnoreCase(answerString)) {
        System.out.println("You don't want a cookie.");
        break;
    } else {
        System.out.println("Answer by saying 'yes' or 'no'");
    }
} while(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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