用字符隐藏单词(hangman)

z0qdvdin  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(255)

我整晚都在努力寻找答案,但什么也没找到。我要做的是生成两个单词,一个秘密单词和一个隐藏单词。隐藏字的长度与秘密字相同,但所有字符都替换为符号。我已经看到了如何使用setbuilder或数组来实现这一点,但是我还不太习惯使用它们,我想知道如果没有它们是否可能。
我很抱歉,如果这似乎漫无边际,但没有一个地方我看有一个解决方案,既不包括数组或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 += '?';
         }
      }

   }
ca1c2owp

ca1c2owp1#

您可以使用string.replaceall()执行此操作:

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

这个 "." 在正则表达式中表示任何字符。

相关问题