Trying to code a Hangman Console Game. But my loop is not stopping when two words are equal. Below is my code. The secret word is "hello". Outcome is under code.
OUTPUTimport java.util.Scanner; public class Hangman { public static void main(String[] args) { int numGuesses = 1; String word; String displayHidden = ""; String used = ""; Game myWord = new Game(); char guess; word = myWord.getWord(); for (int i = 0; i < word.length(); i++) { displayHidden += "*"; } StringBuilder guessedWord = new StringBuilder(word); StringBuilder test = new StringBuilder(displayHidden); System.out.println("Welcome to Hangman... v1.0 - Designed Ashley Bwanya"); System.out.println("This is the secret word you have to guess: \n"); System.out.println(test+ "\n"); do { System.out.print("Guess Letter: "); Scanner scan = new Scanner(System.in); guess = scan.next().charAt(0); for(int i = 0; i < guessedWord.length(); i++){ //check if letter has been guessed if(guessedWord.charAt(i) == guess) { test.setCharAt(i, guess); System.out.println(test); } } }while(guessedWord.equals(test) == false); } }
Welcome to Hangman... v1.0 - Designed Ashley Bwanya
This is the secret word you have to guess:
*****
Guess Letter: h
h****
Guess Letter: e
he***
Guess Letter: l
hel**
hell*
Guess Letter: o
hello
Guess Letter: