This is my first question on this site so i’ll try not to be a total noob..
I’m currently creating hangman game in java.
So my question to you is if we are given a word “ghost”
and ghost is replaced with “_ “,
hiddenWord = ghost.length();
for (i=0; i < ghost.lenth(); i ++)
System.out.print("_ ")
giving us an output of
“_ _ _ _ _ “
Lets say we guess the letter “O”
the letter “o” is guessed, how do i replace `
"_ _ _ _ _" with
"_ _ o _ _ "
my current class file
public void pickWord()
{
String[] listOfWords;
listOfWords = new String[10];
listOfWords[0] = "shenanigans";
listOfWords[1] = "conversely";
listOfWords[2] = "octopus";
listOfWords[3] = "dizzy";
listOfWords[4] = "malicious";
listOfWords[5] = "goosebumps";
listOfWords[6] = "flying";
listOfWords[7] = "staff";
listOfWords[8] = "xylophone";
listOfWords[9] = "zapping";
Random generator = new Random();
int lineNumber = generator.nextInt(9);
disguisedWord = listOfWords[lineNumber];
}
public void displayMark()
{
for( int i = 0; i < disguisedWord.length(); i ++)
underscore = underscore + "_ ";
System.out.println(underscore);
}
public void makeGuess() throws IOException
{
System.out.println("Your word is " + disguisedWord.length() + " letters long.");
System.out.println("Feel free to guess a letter.");
guess = (char)System.in.read();
You can simplify the if statement using the ternary operator: