2016-03-28 55 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); 

正则表达式中的"."表示任何字符。