boolean r = false ; int s = 0 ;
while (r == false) ;
{
s = getInt() ;
if (!(s>=0 && s<=2)) System.out.println ("try again not a valid response") ;
else r = true ;
}
The text never displays itself even when a 3 or a 123 is entered and the loop never terminates. Whats wrong here?
You have a semicolon after the condition. When you use braces to specify a block for your while
you don't use a semicolon.
Remove the ';' after while.
Others have pointed out the bug, but your code is scary in other ways that will eventually trip you up:
if (!(s>=0 && s<=2)) System.out.println ("try again not a valid response") ;
else r = true ;
That's bad because you can easily intend more than one statement to run in the case of the if
or else
clause. Use curly braces and avoid placing conditional statements on a single line:
if (!(s>=0 && s<=2))
{
System.out.println ("try again not a valid response");
}
else
{
r = true;
}
It's easier to read and far less likely to introduce hard-to-see bugs.
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