Text based Java game wont reset properly

57 Views Asked by At

the problem I'm currently facing is that I try to restart the game after you fail/die. In the beginning of the game it asks you for your name, upon entering it you get a "good morning, name" message and are sent to the next class which is waking up in your room. It works at first but when I restart the game, it immediately says "good morning, !" and goes to the room class. I understand that this is some problem with the name variable, but I don't understand what exactly I'm doing wrong.. This is my first question posted, I hope it is not too messy.

Here is my "preparation" class, which is just asking for the name, saying good morning, and then moving to the next class.

public void Preparation() {
        String playerName;
        System.out.println("Please enter your name:");
        playerName = scn.nextLine();
        System.out.println("\nGood morning, " + playerName + "!");
        YourRoom();
    }

This is the YourRoom class.

public void YourRoom() {
        System.out.println("\nYou wake up early, what do you want to do first?");
        System.out.println("1: Sleep in");
        System.out.println("2: Wake up");

And here is an example of a fail/death in a further class.

System.out.println("choice 1 : enter deadly choice here");
        choice = scn.nextInt();
        if (choice == 1) {
            Points = 0;
            System.out.println("You have died, please enter something to restart");
            enterScanner.nextLine();
            Preparation();
            }

However, after entering something to restart the console displays this:

Please enter your name:

Good morning, !

You wake up early, what do you want to do first? 1: Sleep in 2: Wake up

0

There are 0 best solutions below