My question is how can I break out of two loops. Because when you run this progam is asks someone to guess a number from 1 to 1000. Once they guess correct, it asks for then to type "yes" to continuue, and "no" to stop.
Currently when they say no, it breaks out of the inner loop and asks them to guess again. When it's suppose to end the program.
Then I tried:
But that doesn't work either. Thanks for any help.
The loop's condition is checked only before beginning each iteration. If the condition changes to false somewhere within the loop's body, it won't "know" that until it finishes that iteration and checks to see if it should iterate again.
So you need to either restructure your code so that the condition is changed at the bottom of the loop, or you'll need to use a "continue" to get out of the current iteration or "break" to get out of the loop entirely. (Note that if you break out entirely, then your "while" condition might not serve any purpose.)
I don't think it's working fine when the user enters "end." I think it's throwing an exception when your code tries to parse the String "end" as an int. So it's ending, but not gracefully. The reason it tries to parse is because your String comparison is failing.
You shouldn't compare Strings using the == operator. Use String's equals method instead. For example...
if("end".equals(userAnswer))... [ October 18, 2008: Message edited by: marc weber ]
And never write == true, because of the possible confusion with = true. Writing if (xxx = true) is a serious logic error because you are actually changin gthe value of xxx in the "if". The same applies to a while.
You write if (xxx).
Joined: Oct 13, 2005
Two other examples about == and = in these two threads: 12.