2016-05-31 71 views
1
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); 
     } 
     } 
    } 

循环确实保存单词,未知的字符永远不会保存正确的方式。JAVA - Hangman游戏,保持在未知字母输入正确的字母

+0

对不起,忽略updateLetterGuess = letterGuess;它什么也没做,忘记删除它。 –

+0

可能需要查看代码的其余部分,如何存储被猜测的字母? –

+0

我没有看到任何为for循环增加索引之外的变量。你能向我们展示更多你的代码吗? – RamenChef

回答

1

你只打印出latestly猜到了信,因为这是要检查的唯一的事情:你需要以某种方式,有的地方还记得,其中字母先前已经猜到

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]); 
    } 
} 
+0

在那里我正在考虑布尔标志,但是用char []它可能更容易100x更改值。这就是为你过度想象 –

+0

@Aconcagua有无论如何,如果你有时间,我可以真的很快地告诉你?我想我几乎可以拥有它,但如果没问题,我仍然需要帮助? –

+0

@EvanVukasinovic对不起,不,请参阅[这里](http://meta.stackexchange.com/questions/431/any-way-to-send-a-personal-message-to-another-user)或[这里] (http://meta.stackexchange.com/questions/57537/how-do-i-contact-other-users)。只有在这里留下评论的选项 - 或聊天(但现在是2:45,现在我要睡一觉了......)。 – Aconcagua