【问题标题】:Java - Making hangman gameJava - 制作刽子手游戏
【发布时间】:2015-12-10 23:11:49
【问题描述】:

我在尝试制作刽子手游戏时遇到了一个小问题。我之前发表过一篇关于另一个错误的帖子,但现在我遇到了一个我无法弄清楚的新错误。我正在尝试验证字母猜测是否尚未输入。但它跳过了 if/else 语句的整个部分。当我运行这段代码时:

公共类TestingStuff {

static StringBuffer randomWord;
static Scanner console = new Scanner(System.in);
static int totalTries = 1;
static String guess;
static char finalGuess;

public static void main(String[] args) throws Exception {
    randomWord = TestingStuff.sendGet();
    char[] guesses = new char[26];
    int length = randomWord.length();

    System.out.print("* * * * * * * * * * * * * * *"
            + "\n*    Welcome to Hangman!    *"
            + "\n* * * * * * * * * * * * * * *");
    System.out.println("\nYou get 10 tries to guess the word by entering in letters!\n");
    System.out.println(randomWord);
    /*
     Cycles through the array based on tries to find letter
     */
    while (totalTries <= 10) {
        System.out.print("Try #" + totalTries + "\nWord: " + makeDashes(randomWord));

        //Right here: Search through the array of guesses, make it 26 characters to represent the alphabet
        //if the user guess equals an already guessed letter, add to try counter. If it's correct, then reveal the letter that is 
        //correct and do it again without adding to the try counter. 
        System.out.print("\nWhat is your guess? ");
        guess = console.nextLine();
        finalGuess = guess.charAt(0);
        guesses[totalTries - 1] = finalGuess; //Puts finalGuess into the array

            for (int i = 0; i < totalTries; i++) { //checks to see if the letter is already guessed
                if (guesses[i] != finalGuess) {
                    System.out.println(guesses[i]);
                    for (int j = 0; i < length; j++) { //scans each letter of random word
                        if (finalGuess == randomWord.charAt(j)) {
                            //put a method that swaps out dashes with the guessed letter
                            totalTries++;
                        }
                    }
                } else {
                    System.out.println("Letter already guessed, try again! ");
                }
            }
        }
    }

我得到了这样的输出:

* * * * * * * * * * * * * * *
*    Welcome to Hangman!    *
* * * * * * * * * * * * * * *
You get 10 tries to guess the word by entering in letters!

ostracization
Try #1
Word: -------------
What is your guess? a
Letter already guessed, try again! 
Try #1
Word: -------------
What is your guess? 

这只是说当数组中有一个空元素时,字母已经被猜到了。我在这里遗漏了什么吗?

【问题讨论】:

  • if (guesses[i] != finalGuess) { } else { "Letter already guessed, try again! "} 很清楚。如果你想控制一个“空”(什么是空?)元素,那么你必须为它添加一个条件。
  • @m0skit0 当我提示用户输入猜测时,我有它,所以它将把它放入 char 值数组中。这不是我在这里做的吗?

标签: java loops


【解决方案1】:

让我们用你的例子来看看代码(我强烈建议你用调试器自己做):

guesses[totalTries - 1] = finalGuess; // guesses[0] = 'a'
if (guesses[i] != finalGuess) // i = 0, guesses[0] = 'a', finalGuess = 'a'
else System.out.println("Letter already guessed, try again! ");

你可以移动

guesses[totalTries - 1] = finalGuess; //Puts finalGuess into the array

在最外层for 循环的末尾。在处理之前无需存储猜测。

【讨论】:

    【解决方案2】:

    是的。变量totalTries 最初为1。您读取您的猜测,然后将guesses[totalTries - 1] 设置为猜测的字符,这意味着guesses[0] 等于finalGuess。然后你将i从0循环到totalTries - 1,这也是0。循环执行一次,并检查第一个条目不是finalGuess。但它是,我们只是设置它。

    如果您只使用 for 循环来发现重复猜测,您可以将第一个 for 循环中的条件更改为 i &lt; totalTries - 1 并且它应该可以工作,但您需要移动下面的 hangman 单词标记。为尽量减少对代码的影响,请使用 m0skit0 的解决方案。

    【讨论】:

    • 我认为您的修复不起作用,因为问题发生在第一次迭代而不是最后一次迭代。
    • @m0skit0 啊,你是对的,我没有注意到他不仅检查重复的猜测,而且还在那个 for 循环中进行替换。在这种情况下,您的解决方案会更好。我将编辑我的解决方案以更好地反映我的思路。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    相关资源
    最近更新 更多