0

我有一个正在为学校工作的 Hangman 程序,当用户猜错字母时,我无法正确打印错误猜测的数量。这是我到目前为止得到的代码,我将不胜感激任何提示。

import java.util.Scanner;

public class HangmanTest {
    public static void main(String[] args) {

        String[] wordBank = { "madelynn", "crystal", "mcbride", "daughter",
                "adorable", "beautiful", "andrew", "programming", "alyssa",
                "computers", "mcbreezy", "maddy", "happy", "vacation", "beach",
                "java", "benefical", "military", "veteran", "standale",
                "lions", "tigers", "redwings", "pistons", "michigan",
                "football", "baseball", "hockey", "basketball", "golf" };
        int minimum = 0;
        int maximum = wordBank.length - 1;
        String again;

        do {
            int choice = minimum + (int) (Math.random() * maximum);

            String word = wordBank[choice];

            // Converts the random word to asterix
            String userWord = "";
            for (int i = 0; i < word.length(); i++) {
                userWord += "*";
            }

            // Breaks into a bunch of characters
            char[] userWordCh = userWord.toCharArray();

            // Show the random word
            System.out.println("The word for you to guess is " + userWord);

            // instantiate a scanner object
            Scanner input = new Scanner(System.in);

            int size = word.length();
            int rightGuesses = 0;
            int wrongGuesses = 0;

            while (size != rightGuesses) {
                System.out.println("Enter a character: ");
                String response = input.next();
                char ch = response.charAt(0);

                char[] wordChars = word.toCharArray();

                for (int i = 0; i < word.length(); i++) {
                    if (wordChars[i] == ch) {
                        userWordCh[i] = ch;
                        ++rightGuesses;
                    } else {
                        ++wrongGuesses;
                    }
                } // end of for loop

                System.out.print("The word is: ");
                for (int j = 0; j < userWordCh.length; j++)
                    System.out.print(userWordCh[j]);

                System.out.println();
            } // end of while loop

            System.out.println("You had " + wrongGuesses + " wrong guesses.");

            System.out.println("Would you like to play again y/n: ");
            again = input.next();

        } while (again.equals("y"));

    }
}
4

4 回答 4

2
for (int i = 0; i < word.length(); i++) {
    if (wordChars[i] == ch) {
        userWordCh[i] = ch;
        ++rightGuesses;
    } else {
        ++wrongGuesses;
    }
} // end of for loop

在这个循环中,每次猜测与单词中的字母匹配时将 rightGuesses 加 1,每次猜测与单词中的字母不匹配时将 wrongGuesses 加 1。正如你可以想象的那样,这将导致数字集体增加与字母数量相同的数字,而它应该只增加一次。

尝试类似:

boolean foundMatch = false;
for (int i = 0; i < word.length(); i++) {
    if (wordChars[i] == ch) {
        userWordCh[i] = ch;
        if (!foundMatch)
        {
            ++rightGuesses;
            foundMatch = true;
        }
    }
}
if (!foundMatch)
{
    ++wrongGuesses;
}
// end of for loop

现在我们只增加一次 rightGuesses 和 wrongGuesses —— rightGuesses 只能在我们没有找到匹配项的情况下增加(将找到的匹配项设置为 true),而 wrongGuesses 只能在我们没有找到匹配项的情况下增加一次。

于 2013-02-06T21:40:22.083 回答
0

我在这里猜测,但我认为这可能是错误的:

for (int i = 0; i < word.length(); i++) {
     if (wordChars[i] == ch) {
         userWordCh[i] = ch;
         ++rightGuesses;
     } else {
         ++wrongGuesses;
     }
} // end of for loop

这将为匹配/不匹配输入字符的单词中的每个字符增加rightGuesses和变量。wrongGuesses相反,您需要在字符“匹配”时设置一个标志,然后在末尾检查该标志以更新rightGuesseswrongGuesses.

于 2013-02-06T21:41:00.023 回答
0

问题出在你的 for 循环中。您正在迭代每个字母,并且对于每个与您的不匹配的字母,您将其标记为不正确的猜测。如果没有一个字母是正确的,它应该只被标记为不正确。此外,仅当您尚未标记它时,才应将其标记为正确。

import java.util.Scanner;
import java.util.ArrayList;

public class HangmanTest {
    public static void main(String[] args) {

        String[] wordBank = { "madelynn", "crystal", "mcbride", "daughter",
                "adorable", "beautiful", "andrew", "programming", "alyssa",
                "computers", "mcbreezy", "maddy", "happy", "vacation", "beach",
                "java", "benefical", "military", "veteran", "standale",
                "lions", "tigers", "redwings", "pistons", "michigan",
                "football", "baseball", "hockey", "basketball", "golf" };
        int minimum = 0;
        int maximum = wordBank.length - 1;
        String again;

        do {
            int choice = minimum + (int) (Math.random() * maximum);

            String word = wordBank[choice];

            // Converts the random word to asterix
            String userWord = "";
            for (int i = 0; i < word.length(); i++) {
                userWord += "*";

            }

            String guessedLetters="";
            // Breaks into a bunch of characters
            char[] userWordCh = userWord.toCharArray();

            // Show the random word
            System.out.println("The word for you to guess is " + userWord);

            // instantiate a scanner object
            Scanner input = new Scanner(System.in);

            int size = word.length();
            int rightGuesses = 0;
            int wrongGuesses = 0;
            boolean foundLetter;
            char[] wordChars = word.toCharArray();

            guessLoop:
            while (size != rightGuesses) {
                System.out.println("Enter a character: ");
                String response = input.next();
                char ch = response.charAt(0);


                foundLetter=false;
                for (int i=0;i<guessedLetters.size();i++){
                    if (ch == guessedLetters.charAt(i)){
                         System.out.println("Already guessed that letter!");
                         continue guessLoop;
                    }
                }

                guessedLetters+=response;
                for (int i = 0; i < word.length(); i++) {
                    if (wordChars[i] == ch) {
                           foundLetter=true;
                           userWordCh[i] = ch;
                           ++rightGuesses;                            
                    } 
                } // end of for loop
                if(!foundLetter)
                     ++wrongGuesses;
                System.out.print("The word is: ");
                for (int j = 0; j < userWordCh.length; j++)
                    System.out.print(userWordCh[j]);

                System.out.println();
            } // end of while loop

            System.out.println("You had " + wrongGuesses + " wrong guesses.");

            System.out.println("Would you like to play again y/n: ");
            again = input.next();

        } while (again.equals("y"));

    }
}
于 2013-02-06T21:41:08.070 回答
0
for (int i = 0; i < word.length(); i++) {
                if (wordChars[i] == ch) {
                    userWordCh[i] = ch;
                    ++rightGuesses;
                } else {
                    ++wrongGuesses;
                }
            } // end of for loop

这个 for 循环会导致您出现错误猜测的问题,因为它会说一个字符是错误的猜测,即使它在单词中。您可以做的一个选择是在找到字母时将一些布尔值切换为 true。这样,当您离开 for 循环时,您会检查该值是否为真。如果不是,则增加错误的猜测。

于 2013-02-06T21:43:06.730 回答