For some reason a sentinel value will not work as expected
public static int play() {
String input;int result;
do {
input = JOptionPane.showInputDialog("Do you want to play-Enter 1 if yes , 0 if no");
result = Integer.parseInt(input);
} while (result != 1 || result != 0);
return result;
}
This code above never works but it works just fine if i change the condition from while (result != 1 || result != 0); to while (result < 0 || result > 1);
Why is that so and how can i make not equal work in a do...while loop in java ?
Thanks
Use:
while (result != 1 && result != 0) {...}
This will execute the code, ONLY if result is NOT 0 or 1
In your example, the boolean statement in the while loop will ALWAYS be equal to true because result can be equal to only 1 value:
while (result != 0 || result != 1)
If result is 1, then it is not equal to 0, if it is 0, then it can't be 1, so it will ALWAYS be true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With