I'm trying to learn java but I'm stuck trying to do a single program which concerns Do While Statement with two conditions. 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.
(2) user_input
is a Scanner
. You can't compare a scanner to a string. You can only compare a String
to another String
. So you should be using answerString
in your comparison, not user_input
.
(3) Never use ==
to compare strings. StackOverflow has 953,235 Java questions, and approximately 826,102 of those involve someone trying to use ==
to compare strings. (OK, that's a slight exaggeration.) Use the equals
method: 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);
. 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; you need another }
before while
, not after it.
(5) I think you were trying to write a loop that keeps going if the input isn't yes
or no
. Instead, you did the opposite: you wrote a loop that keeps going as long as the input is yes
or no
. Your while
condition should look something like
while (!(answerString.equals("yes") || answerString.equals("no")));
[Actually, it should be equalsIgnoreCase
to be consistent with the rest of the code.] !
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)".
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 {
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);
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.