I am very confused at the behavior of using the || operator on the .equals function. Is there a reason I can not use it on strings or something?
this works:
do{
System.out.println("Play again? [Y/N]");
//input: Y
play = in.nextLine();
play = play.toUpperCase();
}
while(!"Y".equals(input) ); //breaks out of loop (as it should)
why doesn't this work?!
do{
System.out.println("Play again? [Y/N]");
//input: Y
play = in.nextLine();
play = play.toUpperCase();
}
while( !"Y".equals(input) || !"N".equals(input) ); //infinite loop
解决方案
Let's put it into words.
"Eat all of this fruit as long as it's not an apple OR it's not an orange."
Strawberry: not an apple, continue.
Banana: not an apple, continue.
Orange: not an apple, so... continue.
Apple: is apple; but is actually not an orange, so... continue...... :(
If it's an apple, "not an orange" will be true; if it's an orange, "not an apple" will be true; if it's a kiwi, both will be true. There is no way to stop eating (unless you explode or crash into a coma).
Bad logic leads people to death by fruit.
You want "eat all of this fruit as long as it's not an apple AND ALSO not an orange".