Professional Documents
Culture Documents
Hangman Program
Hangman Program
BSECE
Introduction to Java Programming
Hangman Program
import java.util.Scanner;
while (!word.isSolved())
{
System.out.print("(GUESS) Enter a letter in the word ");
System.out.print(word);
System.out.print(" >> ");
char c = input.getNewLetter();
if (!word.update(c)){
totalMistake = totalMistake + 1 ;
System.out.printf("\t'"+ c +"'"+ " is not in the word.");
System.out.println();
}
}
if (word.isSolved()){
System.out.println();
System.out.print("WELL DONE! The word is ");
System.out.print("'"+ word + "'");
System.out.print(". You missed " + totalMistake + " time(s). " );
}
System.out.println();
System.out.println();
System.out.print("Do you want to guess for another word? Enter y or n> ");
Scanner guessAgain = new Scanner(System.in);
char yesorNo = guessAgain.next(".").charAt(0);
boolean answer = yesorNo != 'n';
while (!word.isSolved()){
System.out.print("(GUESS) Enter a letter in the word ");
System.out.print(word);
System.out.print(" >> ");
char c = input.getNewLetter();
if (!word.update(c)){
totalMistake = totalMistake + 1 ;
System.out.printf("\t'"+ c +"'"+ " is not in the word.");
System.out.println();
}
}
if (word.isSolved()){
System.out.println();
System.out.print("WELL DONE! The word is ");
System.out.print("'"+ word + "'");
System.out.print(". You missed " + totalMistake + " time(s). " );
}
System.out.print("Do you want to guess for another word? Enter y or n> ");
yesorNo = guessAgain.next(".").charAt(0);
answer = yesorNo != 'n';
if (answer==false){
System.exit(0);
}
}
System.exit(0);
}