Your nested loops are confusing you. What you have looks like this:
String word = { Get the next word. };
// make while loop to count down the loss of the user
int maxTries = 6;
while (maxTries > 0) {
{ Clear out guesses. Start guesses for this word. }
// Loop once for each letter in the word, plus one extra:
int charCount = word.length();
while (charCount >= 0) {
{ Get the user's guess. }
charCount--;
if ( {User guessed wrong} ) {
maxTries--;
}
}
} When the "charCount" loop exits, the system throws away all of their successful guesses. (...as long as they have some tries left.)
Given one word to guess, why are there two loops instead of one? When thinking about how to design the looping, focus on this: Under what conditions do you want to exit the loop? IE: If the user runs out of incorrect guesses (6 of 'em), exit the loop. They loose. Also, if the user has the whole word, exit the loop. They won.
You might want to use the 'break;' statement to exit a loop.
JeffGrigg
Posting Whiz in Training
218 posts since Aug 2011
Reputation Points: 192
Solved Threads: 28