2016-03-28 54 views
0

我試了整晚找到答案,並沒有設法得到任何東西。我想要做的是產生2個詞,一個祕密詞和一個隱藏的詞。隱藏的單詞與secretWord的長度相同,但所有的字符都用符號替換。我已經看到了如何用setBuilder或者數組來做到這一點,但是我並沒有想到如何使用它們,並且想知道如果沒有它們,這是否可能。隱藏字符(hangman)

對不起,如果這似乎散漫,但沒有在那裏我看過有一個sollution,這並不包括任何數組或setbuilder

public class Hangman { 




    public static void main(String[] args) { 

     Hangman game = new Hangman(); 
     game.initialize("Happiness"); 
     System.out.println("Lets play a round of hangman"); 
     game.playGame(); 

     Scanner keyboard = new Scanner(System.in); 
     char guess = 'a'; 
     int secretLength; 

     public class playGame {{ 
     while (isFound == false) 
      System.out.println("The disguised word is " + game.getDisguisedWord); 
      System.out.println("Guess a letter: "); 
      game.makeGuess(); 
    }} 

    private class isFound {{ 
     if (getSecretword.secretWord.equals(getDisguisedWord.disguisedWord)){ 
     } 
     }} 
     public class getDisguisedWord {{ 
     return disguisedWord; 
     }} 



     public class getSecretWord { 
     initialize.getsecretWord(); 

     } 

     private class makeGuess {{ 
     char guess = keyboard.next().charAt(0); 
     for (int index = 0; index < secretWord.length(); index++) 
      if (initialize.secretWord.charAt(index) == guess) 
       hiddenWord.setCharAt(index, guess); 
     }} 

     public class initialize {{ 
     this.secretWord(); 
     int secretLength = secretWord.length(); 
     while (secretLength > 0){ 
      hiddenWord += '?'; 
     } 
     } 


    } 
+5

你的標籤幾乎拼出了答案。 'String.replace'是你的朋友。嘗試一下。此外,分享您迄今爲止所嘗試的內容,以便人們可以幫助您,並瞭解您卡在哪裏。 –

+0

你的代碼在哪裏? –

+0

http://stackoverflow.com/a/2807731/4297364? –

回答

0

可以使用String.replaceAll()做到​​這一點:

hiddenWord = secretWord.replaceAll(".", yourSymbol); 

正則表達式中的"."表示任何字符。