Skip to content
Advertisement

Basic Java Hangman

I am just starting to learn Java as my first programming language.

In class we were assigned to make a basic Hangman game with the use of a while and for loops.

What I have so far

When the user inputs the first guess it does recognize that that characters that he/she guessed was corrected but just continues on and stating that I have guessed an incorrect letter!

Help would be very appreciated!! My question is what am I doing wrong in my code? I need the program to tell the user if his guess is right or wrong.

My code:

import cs1.Keyboard;

public class Hangman {
    public static void main(String args[]) {
        int guessCount = 0;
        int correctGuess = 0;
        boolean foundIt;
        boolean solved;
        char guess, answer;
        String word;
            
        System.out.println("Welcome to HangMan!");
        System.out.println("Please enter a word for the opponent to guess!");
        word = Keyboard.readString();
        while (guessCount <= 6) {
            System.out.println("Please enter any letter A-Z as your guess!");
            guess = Keyboard.readChar();
            for (int i = 0; i < word.length(); i++) {
                if (guess == word.charAt(i)) {
                    System.out.println("You have guessed a correct letter!");
                    correctGuess++;
                    System.out.println("Correct Guess Count: "
                                        + correctGuess);
                    solved = false;
                }
                else if (guess != word.charAt(i)) {
                    System.out.println("Sorry! That is an incorrect guess! " 
                                       + "Please try again!");
                    guessCount++;
                    System.out.println("Guess Count: " + guessCount);
                    solved = false;
                }
            }
            if (correctGuess == word.length()) {
                solved = true;
                System.out.println("Congratulations! " + 
                                   "You have guessed the word!");
            }                   
        }
    }
}

This is what I have so far and here is the output

Welcome to HangMan!
Please enter a word for the opponent to guess!
hello
Please enter any letter A-Z as your guess!
l
Sorry! That is an incorrect guess! Please try again!
Guess Count: 1
Sorry! That is an incorrect guess! Please try again!
Guess Count: 2
You have guessed a correct letter!
Correct Guess Count: 1
You have guessed a correct letter!
Correct Guess Count: 2
Sorry! That is an incorrect guess! Please try again!
Guess Count: 3
Please enter any letter A-Z as your guess!

Advertisement

Answer

You compare the guess to every character in the String and then display the message for every character. Instead, you should write a method that returns a count of characters that match the input (this also handles words that have repeats of letters). So,

private static int countOf(String in, char ch) {
    int count = 0;
    for (char c : in.toCharArray()) {
        if (c == ch) {
            count++;
        }
    }
    return count;
}

Then you can call it like,

guess = Keyboard.readChar();
int count = countOf(word, guess);
if (count > 0) {
  System.out.println("You have guessed a correct letter!");
  correctGuess += count;
} else {
  System.out.println("Sorry! That is an inncorrect guess! Please try again!");
}
guessCount++;

Edit To do it without a second method you could use,

guess = Keyboard.readChar();
int count = 0;
for (char c : in.toCharArray()) {
    if (c == guess) {
        count++;
    }
}
if (count > 0) {
  System.out.println("You have guessed a correct letter!");
  correctGuess += count;
} else {
  System.out.println("Sorry! That is an inncorrect guess! Please try again!");
}
guessCount++;

And, since you haven’t used the for-each

char[] chars = in.toCharArray();
for (int i = 0; i < chars.length; i++) {
    if (chars[i] == guess) {
        count++;
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement