【问题标题】:Hangman Part 2 with Arrays JavaHangman Part 2 with Arrays Java
【发布时间】:2014-12-24 09:18:50
【问题描述】:

现在在课堂上,我们被分配了另一个使用数组的 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 循环才能将空格更改为正确猜测的字母,但是当您运行程序并输入正确的猜测时,空格不会更改为正确的字母。 这是我的输出

 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;
            }
        }

【问题讨论】:

  • “不工作”不是一个有用的问题描述。 如何“不工作”?编译错误?运行时异常?什么是错误/异常?你谷歌它是为了努力了解它是什么以及它为什么会发生吗?还是不正确的行为?发生了什么?你期望会发生什么?更详细。
  • 还是不太清楚。您是否希望在第二行看到p _ _ _ _ _ _ _ _,在第三行看到p r _ _ _ _ _ ,等等?
  • 好的,我已经更新了它,我认为我真的不能更具体。我正在寻找的是正确猜测的字母来替换“_”空白。
  • 您只需要最初用_ 填充blanks 数组并始终打印其成员,而不是硬编码的下划线。

标签: java arrays loops for-loop


【解决方案1】:

行后

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 变量还没有做任何事情,尽管一旦你到达那一点,你将需要它来处理不正确的猜测..

【讨论】:

    【解决方案2】:

    你很亲密。只是一些调整。

    紧接着

    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]);
    }
    

    【讨论】:

    • 好的,谢谢你的提示,但是用正确猜测的字母替换“_”空白呢?
    猜你喜欢
    • 2016-12-22
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2014-04-22
    • 2014-03-30
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多