if(letterGuessBoolean == true) {
System.out.println("Nice job! That was correct!");
for (position = 0; position < pickRandomWord.length(); position++) {
if (pickRandomWord.charAt(position) == letterGuess) {
System.out.print(letterGuess);
}
else {
System.out.print(unknownLetters);
}
}
}
循环会正确保存单词,未知字符永远不会保存正确的方式。
答案 0 :(得分:1)
您只打印出最新猜测的信件,因为这是您唯一要检查的信件:
pickRandomWord.charAt(position) == letterGuess
你需要记住以某种方式和某个地方,以前猜过哪些字母。一种变体可能如下:
String pickRandomWord = ""; // select your random word
char[] displayOutput = new char[pickRandomWord.length()];
for(int n = 0; n < displayOutput.length; ++n)
displayOutput[n] = '-';
/* ... */
if(letterGuessBoolean) // do not compare against true, if it is already boolean!
{
for(int position = 0; position < pickRandomWord.length(); ++position)
{
if (pickRandomWord.charAt(position) == letterGuess)
{
displayOutput[position] = letterGuess;
}
System.out.print(displayOutput[position]);
}
}