【问题标题】:Java Looping issue in Hangman program(Cant find)Hangman 程序中的 Java 循环问题(找不到)
【发布时间】:2015-12-06 23:00:16
【问题描述】:

我已经完成了大部分程序,但是现在我已经完成了大部分代码,很难找到错误。目前我有多个错误,但我真正需要帮助的主要错误是,如果它是正确的,我的程序将反复循环相同的猜测。它处于无限循环中,我找不到它在哪里。这也引起了我的注意,我的程序将进入负面猜测,因为它应该在它达到 0 时结束。其他一些可以得到帮助的错误是 1)它显示一个正确的猜测作为一个不正确的猜测2)如果有多个,它只能替换密码中的一个字母,它会给我一个错误并结束程序。 & 3)如果我输入9退出,它不会退出。

提前感谢您的帮助。如果需要,我可以添加代码(我只发布主体 ATM。)

public static final int DICTIONARY = 15000;
    public static final int GUESSES = 8;
    public static final int SECRETLENGTH = 20;  



 public static void main(String[] args) {
            int usedSize = 0, randomWord, guesses = GUESSES;
            String word, secretWord, guess, incorrectGuess, correctWord, playAgain;
            char letter;
try
{   
    // Set up connection to the input file
    Scanner hangmanDictionary = new Scanner(new FileReader("dictionary.txt"));
    String [] dictionary = new String [DICTIONARY];
    while (usedSize < DICTIONARY && hangmanDictionary.hasNextLine()) {
        dictionary[usedSize] = hangmanDictionary.nextLine();
        usedSize++;
    }
kbd.nextLine();
            clearScreen();

    randomWord = pickRandom(DICTIONARY);
    word = dictionary[randomWord];

    secretWord = secret(word);

    //comment out when done testing
    System.out.println(word);


    System.out.println("Here is the word to guess: " + secretWord);
    System.out.println("Enter a letter to guess, or 9 to quit.");

    guess = kbd.next();

    do {
        while (!guess.equals("9") || !(guess.equals(word) && guesses > 0)) {
            letter = guess.charAt(0);
            incorrectGuess = "";
            incorrectGuess += letter;
            if (word.indexOf(letter) < 0) {
                guesses--;
                System.out.println("Incorrect guesses: " + incorrectGuess);
                System.out.println("Number of guesses left: " + guesses);
                System.out.println("Enter a letter to guess, or 9 to quit.");
                guess = kbd.next();
            }
            else {
                //FINSH THIS
                correctWord = correctWord(guess, word, secretWord, letter);
                System.out.println(correctWord);
                System.out.println("Incorrect guesses: " + incorrectGuess);
                System.out.println("Number of guesses left: " + guesses);
                System.out.println("Enter a letter to guess, or 9 to quit.");
                guesses--;
            }
        }
            if (guess.equals("9")) {
                System.out.println("Thanks for playing!");
                System.exit(0);
            }
            if (guess.equals(word)) {
                System.out.println("You won!");
            }
            if (guesses == 0) {
                System.out.println("You are out of guesses.");
            }
            System.out.println("Play again? Y/N");
            playAgain = kbd.nextLine().toUpperCase();
    } while (playAgain.equals("Y"));

}

catch (FileNotFoundException e) {
    System.out.println("There was an error opening one of the files.");
}

}

【问题讨论】:

    标签: java arrays eclipse loops


    【解决方案1】:

    这是我的猜测:

    如果用户猜对了,您是否忘记输入guess = kbd.next();

    【讨论】:

      【解决方案2】:

      内部 while 循环是您的主要问题,即考虑输入有效字母(猜测)时会发生什么,在这种情况下,while 循环 OR 条件的第一个条件是 TRUE(假设您没有 9在您的秘密单词中),因此进入 while 循环而不输入 OR 条件的第二部分。之后,您输入 IF 语句的 else 部分(因为它是一个有效的猜测),但在 else 部分您不要求下一个猜测,因此它以相同的猜测返回到 while 循环的开头,因此是无限的循环。

      同样,如果输入9退出!guess.equals("9")计算结果为FALSE,所以第二部分输入OR条件,在第二部分 !(guess.equals(word) &amp;&amp; guesses &gt; 0) 的计算结果为 TRUE(除非密码包含 9),因此您进入了无效的 WHILE 循环。等等……

      尝试使用已知参数编写一小部分代码,然后将它们组合在一起,这样会更容易构建和遵循逻辑。

      【讨论】:

      • 好的,感谢您的意见。我已将 OR 更改为 AND & 现在得到了一些不同的结果。在第一次猜测之后,它现在退出整个循环并开始询问用户是否想再玩一次。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多