Why is ((ans! = 'N') || (ans! = 'Y')) always true?
I am trying to get the user to rotate again and the "while" expression always returns false ... Anyone have any ideas?
reSpin = false;
if (reSpin == false){
System.out.println("Would you like to spin again? Y/N");
char ans = in.next().charAt(0);
if (ans == 'Y'){
reSpin =true;
}else if (ans == 'N'){
System.out.println("Thank you for playing!");
}else {
while ((ans != 'N') || (ans != 'Y')) {
System.out.println("Invalid answer, please only enter Y/N");
System.out.println("Would you like to spin again? Y/N");
ans = in.next().charAt(0);
}
}
}
source to share
||
returns true if any of the expressions on both sides are true.
If ans
- Y
, then ans != 'N'
true, so the whole expression is (ans != 'N') || (ans != 'Y')
true. If ans
- N
, then ans != 'Y'
true, so the whole expression is true.
You want (ans != 'N') && (ans != 'Y')
one that says " ans
not 'N'
and ans
also not 'Y'
."
source to share