Hangman Part 2 with Arrays Java

时间:2014-10-28 19:54:10

标签: java arrays loops for-loop

现在在课堂上我们被分配了另一个使用Arrays的Hangman项目,我们必须生成“_”。 当用户输入“wordAnswer”时,我已成功生成所有空白。 我的问题是我无法弄清楚如何用空白“_”替换正确的猜测字符。

这是我整个程序的代码,我知道它并非完整。

import cs1.Keyboard;
public class Game {

    public static void main(String[] args) {
        int correctGuess = 0;
        int wrongGuess = 0;
        int bodyParts = 0;
        char guess;
        boolean foundIt;
        System.out.println("Welcome to Hangman!");
        System.out.println("Please enter a word for the other user to guess!");
        String wordAnswer = Keyboard.readString();
        char[] chars = wordAnswer.toCharArray();
        char[] blanks = new char[wordAnswer.length()];

        System.out.println("You have 6 attempts to guess the word!");
        do {
            for (int i = 0; i < wordAnswer.length(); i++) {
                System.out.print(" _ ");
            }
            System.out.println("Please enter in your guess!");
            guess = Keyboard.readChar();
            for (int i = 0; i < chars.length; i++) {
                if (guess == chars[i]) {
                    blanks[i] = guess;
                    foundIt = true;
                }
            }
        } while (correctGuess < wordAnswer.length() && wrongGuess != 6); {
            System.out.println("Congratulations! You have solved the word!");
        }
    }
}

我知道您必须使用for循环才能将空白更改为正确的猜测字母,但是当您运行程序并输入正确的猜测时,空白不会更改为正确的字母。 这是我的OutPut

 Welcome to Hangman!
Please enter a word for the other user to guess!
program
You have 6 attempts to guess the word!
 _  _  _  _  _  _  _ Please enter in your guess!
p
 _  _  _  _  _  _  _ Please enter in your guess!
r
 _  _  _  _  _  _  _ Please enter in your guess!
o
 _  _  _  _  _  _  _ Please enter in your guess!
g
 _  _  _  _  _  _  _ Please enter in your guess!
r
 _  _  _  _  _  _  _ Please enter in your guess!
a
 _  _  _  _  _  _  _ Please enter in your guess!
m
 _  _  _  _  _  _  _ Please enter in your guess!

我期待的是!!

    Welcome to Hangman!
Please enter a word for the other user to guess!
program
You have 6 attempts to guess the word!
 _  _  _  _  _  _  _ Please enter in your guess!
p
 p  _  _  _  _  _  _ Please enter in your guess!
r
 p  r  _  _  _  _  _ Please enter in your guess!
o
 p  r  o  _  _  _  _ Please enter in your guess!

等...

这就是我所拥有的,但提示和帮助会非常感激!!

System.out.println("Please enter in your guess!");
        guess = Keyboard.readChar();
        for (int i = 0; i < chars.length; i++) {
            if (guess == chars[i]) {
                blanks[i] = guess;
                foundIt = true;
            }
        }

2 个答案:

答案 0 :(得分:0)

行后

char[] blanks = new char[wordAnswer.length()];

添加以下内容:

for(int i = 0; i < wordAnswer.length(); i++) {
    blanks[i] = '_';
}

然后,代替System.out.print(" _ "),执行System.out.print(" " + blanks[i] + " ");

您知道,您需要修复do ... while循环之后的内容。它将错误地告诉该人他们已经成功地解决了这个词,即使他们没有成功。一段时间后你也不需要{ ... }。此外,您的foundIt变量尚未执行任何操作,但是一旦达到这一点,您将需要它来处理错误的猜测..

答案 1 :(得分:0)

你非常接近。只需进行一些调整。

之后
char[] blanks = new char[wordAnswer.length()];

添加

Arrays.fill(blanks, "_");

或者,如果您不被允许使用Arrays课程,那么请按照以前的方式进行:

for (int i = 0; i < blanks.length; i++) {
   blanks[i] = " _ ";
}

而不是

for (int i = 0; i < wordAnswer.length(); i++) {
   System.out.print(" _ ");
}

for (int i = 0; i < blanks.length; i++) {
      System.out.print(blanks[i]);
}