用字符隐藏一个单词(hangman)

时间:2016-03-28 06:05:59

标签: java string replace

我已经整夜尝试找到答案而且没有设法得到任何东西。我想要做的是生成2个单词,一个secretWord和一个hiddenWord。隐藏的单词与secretWord的长度相同,但所有字符都用符号替换。我已经看过如何使用setBuilder或数组执行此操作,但我不习惯使用它们,并且想知道没有它们是否可行。

我很抱歉,如果这似乎是漫无目的,但我没有看到有一个不包括数组或setbuilder的sollution

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 += '?';
         }
      }


   }

1 个答案:

答案 0 :(得分:0)

您可以使用String.replaceAll():

来完成
hiddenWord = secretWord.replaceAll(".", yourSymbol);

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