0% found this document useful (0 votes)
147 views

Lab Exer 6 A

This Java program implements a simple hangman game where the user tries to guess the letters in a hidden word ("EDUCATION"). It uses methods to check if the guessed letter is in the solution, locate its position, and update the word with correct guesses. The program displays the current word, takes guesses, and notifies if the guess is right or wrong until the full word is revealed, at which point it congratulates the user.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

Lab Exer 6 A

This Java program implements a simple hangman game where the user tries to guess the letters in a hidden word ("EDUCATION"). It uses methods to check if the guessed letter is in the solution, locate its position, and update the word with correct guesses. The program displays the current word, takes guesses, and notifies if the guess is right or wrong until the full word is revealed, at which point it congratulates the user.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

*;
import java.util.Scanner;

public class Main{


public static void main(String[] args){
char guess;

Scanner input = new Scanner(System.in);

String phraseSolution = "EDUCATION";


StringBuilder phrase = new StringBuilder("ED??A??ON");

System.out.print("Current Word: " + phrase + "\n");

while(!phraseSolution.equals(phrase.toString())){

System.out.print("Enter your guess letter: ");


guess = Character.toUpperCase(input.nextLine().charAt(0));

boolean check = check(guess, phraseSolution);


if (check){
int spot = placeLocator(guess, phraseSolution);
phrase.setCharAt(spot, guess);
System.out.println(phrase);

System.out.println("Correct! \n\nEnter guess letter again");


}

else{
System.out.println("Sorry! This letter is not in the word please
guess another!\n");
}
}

System.out.print("\n\nCongrats! You found all letters and finished the


game!");
}

public static int placeLocator(char input, String phraseSolution){


int place;
int x = 0;
while(input != phraseSolution.charAt(x)){
x++;
}

place = x;
return place;
}

public static boolean check(char input, String phraseSolution){


boolean validation = false;
int letter = 0;

for(int x = 0; x < phraseSolution.length(); x++){


if(input != phraseSolution.charAt(x)){
validation = false;
}

else {
validation = true;
letter++;
}
}

if(letter >= 1) {
validation = true;
}

return validation;
}
}

You might also like