212k views
5 votes
I have a java hangman game and want to add a play again option how do i do that?

please help its worth 30 percent of my grade

User Tomas Giro
by
8.8k points

1 Answer

1 vote

Answer:

I'm not sure if this is Java-like (code.org) or an actual compiler you use. But the best way to do it is to use a while loop that continues running the game until the player chooses to exit the game.

Step-by-step explanation:

public class Hangman {

public static void main(String[] args) {

boolean playAgain = true;

while (playAgain) {

//This asks the player if they want to play again.

System.out.println("Do you want to play again? (y/n)");

Scanner scanner = new Scanner(System.in);

String input = scanner.nextLine();

if (input.equals("n")) {

playAgain = false;

}

}

}

}

(Rough example haven't done java in a min but it could work?)

User Philip McQuitty
by
8.6k points