You are on page 1of 3

Kryzchel Anne Dela Cruz

BSECE
Introduction to Java Programming
Hangman Program
import java.util.Scanner;

public class Hangman {


public static void main( String[] args) throws Exception{

Scanner scan = new Scanner( System.in);


int totalMistake = 0;
boolean b = false;
SecretWord word = new SecretWord();
KeyboardInput input = new KeyboardInput();

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 (answer== true){

scan = new Scanner( System.in);


totalMistake = 0;
b = false;
word = new SecretWord();
input = new KeyboardInput();

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);
}

You might also like