我在项目的最后一步,只有一个小错误,我似乎无法解决。
我的目标是不打印“到目前为止的知识:” + idk)行;如果while循环完成。
我试图将其移动到不同的行,但是似乎没有任何用处。
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.");
}
}
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.
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.
答案 0 :(得分:0)
最简单的方法是:
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.");
在打印之前只需检查条件是否满足
答案 1 :(得分:0)
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.");
}
答案 2 :(得分:0)
很酷……这样做:
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.");
}
因此,一旦您意识到满足停止条件,便可以从根本上打破循环。您确实调用了guess()函数,但是在打印语句之前就跳出了循环。
答案 3 :(得分:0)
您可以将其翻转为do ... while循环。
// 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 + "\"" + "!");