Java - Making hangman game

2020-05-09 09:12发布

I'm having a slight problem when trying to make a hangman game. I made a post regarding a different error before, but now I am running into a new one that I can't figure out. I'm trying to verify that the letter guess wasn't already entered. But it is skipping an entire section of the if/else statement. When I run this code:

public class 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! ");
                }
            }
        }
    }

I'm getting an output of this:

* * * * * * * * * * * * * * *
*    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? 

It's just saying that the letter is already guessed when there is an empty element in the array. Am I missing something here?

标签: java loops
2条回答
唯我独甜
2楼-- · 2020-05-09 09:59

Let's walk through the code with your example (I strongly suggest you do this yourself with a debugger):

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! ");

You can just move

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

at the end of the outermost for loop. There's no need to store a guess before having processed it.

查看更多
▲ chillily
3楼-- · 2020-05-09 10:08

Yes. The variable totalTries is initially 1. You read your guess and then you set guesses[totalTries - 1] to the character guessed, meaning guesses[0] is equal to finalGuess. Then you loop over i from 0 to totalTries - 1 which is also 0. The loop is executed once and it is checked that the first entry is not finalGuess. But it is, we just set it.

If you use the for loop only to discover duplicate guesses, you could change the condition in the first for-loop to i < totalTries - 1 and it should work, but you need to move marking the hangman word below. For a minimal impact to your code, use m0skit0's solution.

查看更多
登录 后发表回答