2015-09-30 33 views
0

我正在嘗試編寫財富遊戲輪的程序,並且我希望它只是在用戶輸入的單詞中想要開始遊戲,然後再進行猜測。到目前爲止,我有這個。財富循環之輪簡單

String enterPhrase = (" Enter a phrase : ");  
    String guess = ""; 
    Scanner input = new Scanner (System.in); 
    boolean notDone = true; 
    while (notDone) 
    { 
     notDone = false; 
     for(char guessLetter : enterPhrase.toCharArray()){ 

      if(guess.indexOf(guessLetter) == -1){ 
       System.out.println('*'); 
       notDone = true; 

      } else { 

        System.out.println(guessLetter); 
        } 
      } 
       if(! notDone) {break;} 
        System.out.println("Guess a letter: "); 
        String letter1 = input.next(); 
        guess += letter1; 



    } 
    System.out.println("Hooray! It took you guesses"); 
    } 
} 

,並打印出這一點:

Guess a letter: 
d 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 

如果我能得到關於如何使破折號水平打印一些幫助。並且在他們猜測的每個字母上都有輸入,如果正確並且在程序結束時計算他們完成單詞需要多少次。

+2

使用'System.out的增加此計數器.print'而不是'System.out.println' –

回答

0

System.out.println()將一個字符串打印到控制檯,並將一個末尾連接到字符串。

如果你想水平打印破折號,那麼你將需要使用System.out.print() - 這不會連接端線。這應該用於打印猜測的單個字母(但顯然不是在打印「猜字母」時)。

要計算有多少猜測要求用戶得到整個字符串正確的,只是存儲的另一個變量,int count,初始化爲0,他們猜測每次由1

+0

我在:if(!notDone){break;}'code'中添加了:int count'code',它將一個字母放入短劃線中,我如何得到它把其他字母,當它再次運行到另一個破折號,而保持第一個那裏 –

+0

有很多方法可以做到這一點。我該怎麼做才能記住數組或字符串中猜到的字符。這樣你就不會覆蓋以前的猜測,而只是增加了他們。這可能是最簡單的字符串連接。 – NoseKnowsAll