简体   繁体   中英

How do I make the last line not print if my while loop is successful?

I'm on the last bit of my project and theres only 1 small error that I can't seem to fix.

My goal is to not print the line "Your knowledge so far: " + idk); if the while loop is completed.

I've attempted to move it around to different lines but nothing seems to do the trick.

    char guess = getGuess(console);
    String idk = replaceBlanks(secretWord, blanks, guess);
    System.out.println("Your knowledge so far: " + idk);
    int counter = 1;
    while (!(idk.equals(secretWord))) {
        counter ++;

        guess = getGuess(console);

        idk = replaceBlanks(secretWord, idk, guess);
     System.out.println("Your knowledge so far: " + idk);

    }
    System.out.println("Congratulations, you discovered the word " +  "\"" + idk + "\"" + "!");
    System.out.println("It took you " + counter + " guesses.");

  }
}

EXPECTED

Your knowledge so far: _______
Guess a letter: r
Your knowledge so far: R_____R
Guess a letter: a
Your knowledge so far: RA___AR
Guess a letter: c
Your knowledge so far: RAC_CAR
Guess a letter: e
Congratulations, you discovered the word "RACECAR"!
It took you 4 guesses.

ACTUAL

Your knowledge so far: _______
Guess a letter: r
Your knowledge so far: R_____R
Guess a letter: a
Your knowledge so far: RA___AR
Guess a letter: c
Your knowledge so far: RAC_CAR
Guess a letter: e
Your knowledge so far: RACECAR    <<<<<<<<<<<<-------
Congratulations, you discovered the word "RACECAR"! 
It took you 4 guesses.

Easiest approach would be this:

while (!(idk.equals(secretWord))) {
  counter ++;

  guess = getGuess(console);

  idk = replaceBlanks(secretWord, idk, guess);
  if (!idk.equals(secretWord)) {
    System.out.println("Your knowledge so far: " + idk);
  }
}
System.out.println("Congratulations, you discovered the word " +  "\"" + idk + "\"" + "!")
System.out.println("It took you " + counter + " guesses.");

Just checking if the condition is not met before printing

char guess = getGuess(console);
String idk = replaceBlanks(secretWord, blanks, guess);
System.out.println("Your knowledge so far: " + idk);
int counter = 1;
while (!(idk.equals(secretWord))) {
    counter ++;
    System.out.println("Your knowledge so far: " + idk);

    guess = getGuess(console);

    idk = replaceBlanks(secretWord, idk, guess);

}
System.out.println("Congratulations, you discovered the word " +  "\"" + idk + "\"" + "!");
System.out.println("It took you " + counter + " guesses.");

}

Cool ... do it like this :

char guess = getGuess(console);
String idk = replaceBlanks(secretWord, blanks, guess);
System.out.println("Your knowledge so far: " + idk);
int counter = 1;
while (true) {
    counter ++;

    guess = getGuess(console);

    idk = replaceBlanks(secretWord, idk, guess);
if( idk.equals(secretWord)) break ;
 System.out.println("Your knowledge so far: " + idk);

   }
    System.out.println("Congratulations, you discovered the word 
" +  "\"" + idk + "\"" + "!");
System.out.println("It took you " + counter + " guesses.");

}

So you basically break out of the loop once you realise your stop condition is met. You do call the guess() function, but you break out of the loop , just before printing the statement.

You can flip it around to a do...while loop instead.

// not sure about this... idk should look like "_____" at this point.
String idk = replaceBlanks(secretWord,blanks,null);
int counter = 0;
do {
    System.out.println("Your knowledge so far: " + idk);
    counter ++;
    guess = getGuess(console);
    idk = replaceBlanks(secretWord, idk, guess);
} while (!(idk.equals(secretWord)));
System.out.println("Congratulations, you discovered the word " +  "\"" + idk + "\"" + "!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM