Example program output (user input in green):
Welcome to the Hangman game! ===================== Word to guess: **** Guessed characters: Lives left: 6 Enter a character: e Wrong guess! ===================== Word to guess: **** Guessed characters: e Lives left: 5 Enter a character: a Good guess! ===================== Word to guess: *a*a Guessed characters: ea Lives left: 5 Enter a character: j Good guess! ===================== Word to guess: Ja*a Guessed characters: eaj Lives left: 5 Enter a character: v Good guess! Congratulations! You've guessed the word: JavaCheck if the user enters only one character. If the user enters more than one character, print a message and ask for a new character:
Welcome to the Hangman game!
=====================
Word to guess: ****
Guessed characters:
Lives left: 6
Enter a character: abcdef
Please enter a single character.
=====================
Word to guess: ****
Guessed characters:
Lives left: 6
Enter a character:
Tell the user if they have already guessed the character:
Welcome to the Hangman game! ===================== Word to guess: **** Guessed characters: Lives left: 6 Enter a character: j Good guess! ===================== Word to guess: J*** Guessed characters: j Lives left: 6 Enter a character: J You already guessed that character. ===================== Word to guess: J*** Guessed characters: j Lives left: 6 Enter a character:
String
from the Java standard library:
indexOf
contains
toLowerCase
toUpperCase
haystack
contains needle
, ignoring differences in upper or lower case: boolean containsIgnoreCase(String haystack, char needle)
char toLowerCase(char ch)
String
is allowed:
charAt
length