【问题标题】:a simple Guess letters game in java一个简单的Java猜字母游戏
【发布时间】:2018-05-12 07:04:46
【问题描述】:

我刚刚开始写一个(猜字母游戏)作为作业.. 在 java 中(第一个玩家输入 10 个单词,然后隐藏 3 个随机字符,之后第二个玩家将猜测每个字符,如果他猜对了然后将显示第一个玩家的一个新单词来猜测隐藏的字符,直到所有 10 个单词都猜对了。如果他猜错了,他只有 10 次尝试退出程序>>>>问题在于替换第二个玩家猜到的隐藏角色


这里我如何隐藏 3 个随机字符形成一个字符串:

Scanner s = new Scanner(System.in);


       Random r= new Random();
  String word= null;
    for (int i=0; i<10;i++){
                 word= s.nextLine();

    }
    int wordlength = word.length();
    int a=0, b=0,c=0;
    while(a==b||b==c||a==c){
    a= r.nextInt( wordlength);
     b= r.nextInt( wordlength);
    c= r.nextInt( wordlength);        
    }

    char[] wordsArray = word.toCharArray();
    wordsArray [a]='-';
            wordsArray [b]='-';
    wordsArray [c]='-';

    String modwords =new String (wordsArray);
    System.out.println(modwords );

【问题讨论】:

  • 那么问题是什么?
  • 问题是如果用户输入正确,如何用用户输入替换隐藏的

标签: java replace replaceall


【解决方案1】:

最好将您需要做的事情分解成更小的步骤:

  1. 从一号玩家那里得到10个字
  2. 对于每个单词,使用该单词玩一轮游戏

让我们将这些步骤命名为 getWordsplayRound

从第一步开始,我们想要得到一个包含 10 个单词的列表:为此,我们可以使用 List&lt;String&gt;

让我们为它创建一个方法:

private static List<String> getWords( int numberOfWords )
{
    Scanner s = new Scanner( System.in );
    List<String> words = new ArrayList<String>();
    for ( int i = 0; i < numberOfWords; i++ )
    {
        words.add( s.nextLine() );
    }
    return words;
}

现在我们可以调用这个方法来获取我们的单词列表,并将它们一次一个单词提供给我们的第二个方法:

public static void main( String[] args )
{
    int numberOfWords = 10;
    System.out.println( "Player 1: enter " + numberOfWords + " words" );

    List<String> words = getWords( numberOfWords )

    System.out.println( "Ready, player 2?" );

    for ( String word : words )
    {
        playRound( word );
    }
}

所以现在我们有一个更简单的问题——打一个回合,它涉及以下步骤:

  1. 选择三个我们要屏蔽字母的位置
  2. 创建一个单词的蒙版版本,其中选定的字母被划掉,并将其显示给玩家二
  3. 玩家二猜一个字母:
  4. 猜对了吗?如果是这样,请将相应的破折号替换为该字母
  5. 从第 3 步开始重复,直到所有破折号消失

让我们将前三个步骤命名为方法:

private static List<Integer> getMaskPositions( String word ) { ... }

private static StringBuilder createdMaskedWord( String word, List<Integer> maskPositions ) { ... }

private static char getNextGuess( ) { ... }

然后我们可以在playRound() 方法中使用这些:

private static void playRound( String word )
{
    // choose three positions to mask out:
    List<Integer> maskPositions = getMaskPositions( word );

    // mask our word by dashing out the three chosen positions:
    StringBuilder maskedWord = createdMaskedWord( word, maskPositions );

    System.out.println( "The next word is: " + maskedWord );

    while ( maskedWord.toString().contains( "-" ) )
    {
        // fetch a one-character guess from Player 2:
        char guess = getNextGuess();

        for ( Integer position : maskPositions )
        {
            if ( word.charAt( position ) == guess )
            {
                // guess is correct: replace the dash with the right letter
                maskedWord.setCharAt( position, guess );
            }
        }
        // now re-display the masked word (showing any correctly-guessed letters):
        System.out.println( maskedWord );
    }

    // no more dashes left: all missing letters correctly guessed:
    System.out.println( "Well Done!" );
}

这仍然需要实现三个方法:getMaskPositions()createdMaskedWord()getNextGuess(),但现在这些已经很多了更简单的问题供您解决(并且您可以重复使用您已经编写的一些代码)。

请注意,我们使用 StringBuilder 来存储 maskedWord:这允许我们更改字母(不能使用不可变的 String 进行此操作)。

您还可以通过检查有效输入来改进我的代码:例如,每个单词不得短于 3 个字母。

剩下的交给你:)

【讨论】:

    【解决方案2】:

    将用户输入读取到StringBuilder并比较abc的位置,如果用户输入的char等于原始单词char而不是替换它,类似的东西

    public static void main(String[] args) {
        String word = "hello";
        StringBuilder obscureWord = new StringBuilder("h-l-o");
        Scanner s = new Scanner(System.in);
    
        while (true){
            int pos = obscureWord.indexOf("-");
            if (pos == -1)
                break;
    
            System.out.println("guess the letter " + obscureWord + " : ");
            char c = s.nextLine().charAt(0);
            if (c == word.charAt(pos)){
                obscureWord.replace(pos,pos+1, word.substring(pos,pos+1));
                System.out.println("correct guess! " + obscureWord);
            }
            else {
                System.out.println("incorrect, try again");
            }
        }
    }
    

    当然,您可以将 while 替换为 for loop 以进行限制尝试

    【讨论】:

    • @Aloosha,将答案修改为完整示例,请以更清晰的方式说明您的要求...
    • look>>> 如果隐藏字符后的单词 (Hello) 看起来像 (H-l--) 用户例如猜测字母 e >> 我想显示这样的结果 (Hel- -)
    • @Aloosha,好的,现在我明白了...修改答案
    • 知道了..放在这里应该是一个初始化的词..如果我想让用户输入词和其他用户猜怎么办...对不起,但我是初学者.. .
    • @Losha 告诉我你到目前为止得到了什么......这是解决你发布的问题的一种方法,你需要将它插入到你的代码中,将word 替换为获取第一个用户输入和obscureWord 具有选择随机字母的功能,并像以前一样用- 替换它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2013-03-27
    • 2014-09-10
    • 2022-12-09
    • 1970-01-01
    • 2013-03-24
    相关资源
    最近更新 更多