Hangman in Java

What it does: 

A simple Java Hangman console program. One word to be guessed, where it will record how many times the attempts to get the answer and how many correct letters the user has got so far.

Code:


public class Hangman
{
   public static void main(String[] args)
   {
      Scanner keyboard = new Scanner(System.in);

      int  noOfGuesses =0,lettersCorrect=0;
      String choiceToPlay, userGuess;
      char menuChoice, letter;
      char [] guessWord = {'h', 'e', 'l', 'l', 'o'};
      char [] correctLetter = {'-', '-', '-', '-', '-'};

      System.out.println("Hello and welcome to Hangman!");

      //main game menu
      do
      {  System.out.println("Do you want to play? Press Y/N\n");
         choiceToPlay = keyboard.next();
         menuChoice = choiceToPlay.charAt(0);

         if(!(menuChoice == 'y'||menuChoice == 'Y'||menuChoice == 'n'||menuChoice =='N'))
         {
           System.out.println("You have entered an invalid option. Try again!\n");
         }//if invalid

      }//do
      while (!(menuChoice =='Y' ||menuChoice=='y'||menuChoice == 'n'|| menuChoice =='N'));

      if(menuChoice == 'N'||menuChoice =='n')
      {
         System.out.println("You have chosen to leave the game.");
         System.out.println("Goodbye!");
         System.exit(0);
      }//if

      else
      {

         while(lettersCorrect <5)
         {
            //Getting users guess.
            System.out.println("\nThe guess word has 5 letters.");
            System.out.println("Enter a letter to guess: ");

            userGuess = keyboard.next();
            letter = userGuess.charAt(0);

            //Incrementing letters each time.
            noOfGuesses++;

            if(letter == guessWord[0])
            {
               System.out.println("There is 1 H in the word");
               System.out.println("You have guessed the first letter correctly.");
               correctLetter[0] = letter;
               System.out.println(correctLetter);
               System.out.println("You have had " + noOfGuesses + " guesses, so far");

               lettersCorrect++;

               System.out.println("Letters correct so far: "+lettersCorrect);
            }//if first letter

            else if(letter==guessWord[1])
            {
               System.out.println("There is 1 E in the word");
               System.out.println("You have guessed the second letter correctly.");

               correctLetter[1] = letter;
               System.out.println(correctLetter);
               System.out.println("You have had " + noOfGuesses + " guesses, so far");

               lettersCorrect++;

               System.out.println("Letters correct so far: "+lettersCorrect);
            }//if second letter

            else if(letter ==guessWord[2]||letter==guessWord[3])
            {
               System.out.println("There are 2 L's in the word");
               System.out.println("You have guessed the third and fourth letters correctly.");

               correctLetter[2] = letter;
               correctLetter[3]=letter;
               System.out.println(correctLetter);
               System.out.println("You have had " + noOfGuesses + " guesses, so far");

               lettersCorrect+=2;

               System.out.println("Letters correct so far: "+lettersCorrect);

            }//if third and fourth letters

            else if(letter ==guessWord[4])
            {
               lettersCorrect++;
               System.out.println("There is 1 O in the word");
               System.out.println("You have guessed the fifth letter correctly.");
               correctLetter[4] = letter;
               System.out.println(correctLetter);
               System.out.println("You have had " + noOfGuesses + " guesses, so far");
               System.out.println("Letters correct so far: "+lettersCorrect);
            }//if fifth letter

            else
            {
               System.out.println("The letter you guessed is not in the word.\n");
               System.out.println("Guesses taken so far: "+noOfGuesses);
               System.out.println("Letters correct so far: "+lettersCorrect);
            }//else incorrect letter

         }//while loop

         System.out.println("\nYou found the word!");
         System.out.println("It was hello.");
         System.out.println("Total guesses: "+noOfGuesses);
         System.exit(0);

      }//else
   }//main
}//class

One thought on “Hangman in Java

  1. I’m impressed, I must say. Seldom do I encounter a blog that’s both
    educative and engaging, and let me tell you, you have hit the nail on the head.
    The problem is an issue that not enough folks are
    speaking intelligently about. I am very happy that I came across this in my search for something relating to this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s