import java.util.Scanner;
class Main {
public static void main(String[] args) {
int tries = 0;
boolean iterated = false;
String temp = "";
String holder = "";
System.out.println("Welcome to the guessing game! Guess one letter at a time. You have 5 incorrect guesses remaining.");
Scanner reader = new Scanner(System.in);
System.out.println("The Secret Word Is: ______ (6)");
String word = "sleepy";
do {
System.out.print("Your Guess: ");
String guess = reader.nextLine();
for(int i = 0; i < word.length(); i ++) {
if (guess.equals(Character.toString(word.charAt(i)))) {
if(!iterated)
temp += Character.toString(word.charAt(i));
else {
holder = Character.toString(temp.charAt(i)).replace("-", guess);
temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
}
} else {
if(!iterated) {
temp += "-";
}
}
}
tries++;
iterated = true;
System.out.println(temp);
if(temp.equals(word)) {
System.out.println("You guessed correctly!");
break;
}
}while (tries < 5);
}
}
So this is the code I have so far. It works pretty well but I was wondering how I could make it so that every time you add enter a letter, it tells you whether it was right or wrong and how many tries you have remaining. Also I was wondering how i could make it so that if the user doesn't guess the word I can say something like "sorry you didn't guess it. the word was (sleepy)". Thank you!
hangman
tag to your post. \$\endgroup\$