crpittman18
crpittman18

Reputation: 1

How do I make this number guessing game restart once you guess the number right?

I can't figure out how to make the game restart itself. I think I need to put something where it says System.out.println("Ok, let's play again."); What do I need to put there to restart it? What am I missing?

import java.util.Random;
import java.util.Scanner;

public class GuessMyNumber {

    public static void main(String[] args) {

        Random Bob = new Random();
        int NumToGuess = Bob.nextInt(20);
        int NumOfTries = 0;
        Scanner Bill = new Scanner(System.in);
        int guess;
        boolean win = false;

        while (win == false) {

            System.out.println("I'm thinking of a number between 1 and 20, can you guess it?");
            guess = Bill.nextInt();
            NumOfTries++;

            if (guess == NumToGuess) {
                win = true;
            }
            else if (guess < NumToGuess) {
                System.out.println("Your guess is too low");
            }
            else if (guess > NumToGuess) {
                System.out.println("Your guess is too high");
            }
        }

        System.out.println("Congratulations!");
        System.out.println("The number was " + NumToGuess);
        System.out.println("It took you " + NumOfTries + " tries"); 

        while (win == true) {
            String YesOrNo;
            YesOrNo = Bill.nextLine();
            System.out.println("Would you like to play again? " + YesOrNo);
            YesOrNo = Bill.nextLine();

            if (YesOrNo.equalsIgnoreCase("yes"))
            {
                System.out.println("Ok, let's play again.");
            }
            else
            {
                System.out.println("Ok, maybe next time.");
            }
        }
    }
}

Upvotes: 0

Views: 1603

Answers (4)

mezzodrinker
mezzodrinker

Reputation: 988

I would wrap the whole method body into a while loop that checks if a flag (e.g. boolean continueGame) is true. I mean something like this:

import java.util.Random;
import java.util.Scanner;

public class GuessMyNumber {
    public static void main(String[] args) {
        Random Bob = new Random();
        Scanner Bill = new Scanner(System.in);
        int guess;
        boolean continueGame = true; // the flag (boolean) that allows us to check if the user wants to continue the game or not

        while(continueGame) {
            boolean win = false;
            int NumToGuess = Bob.nextInt(20); // moved into the while loop because they need to be initialized every time you start the game
            int NumOfTries = 0;

            while (win == false) {

                System.out.println("I'm thinking of a number between 1 and 20, can you guess it?");
                guess = Bill.nextInt();
                NumOfTries++;

                if (guess == NumToGuess) {
                    win = true;
                }
                else if (guess < NumToGuess) {
                    System.out.println("Your guess is too low");
                }
                else if (guess > NumToGuess) {
                    System.out.println("Your guess is too high");
                }
            }

            System.out.println("Congratulations!");
            System.out.println("The number was " + NumToGuess);
            System.out.println("It took you " + NumOfTries + " tries"); 

            // As CandiedOrange already noted, you need to remove the while loop which was here.
            String YesOrNo;
            YesOrNo = Bill.nextLine();
            System.out.println("Would you like to play again? " + YesOrNo);
            YesOrNo = Bill.nextLine();

            if (YesOrNo.equalsIgnoreCase("yes"))
            {
                System.out.println("Ok, let's play again.");
                continueGame = true;
            }
            else
            {
                System.out.println("Ok, maybe next time.");
                continueGame = false;
            }
        }
    }
}

That's about it. If you have a question about this answer, feel free to ask.

Upvotes: 0

candied_orange
candied_orange

Reputation: 7334

Your second loop is the problem. You likely copied the first loop. In the second loop you set YesOrNo not win. So it will loop forever.

   while (win == true) {          //You test win
        String YesOrNo;
        YesOrNo = Bill.nextLine() //You set YesOrNo
        ...
   }                              //You never set win to anything new so it loops forever 

Typical way to solve the restart problem is to use one loop not two and have the user enter -1 when they want to quit. That way the game keeps going win or lose until they are done.

By the way, we use camelCase in java so YesOrNo should be yesOrNo but that's just a style issue.

Upvotes: 0

CannotCode
CannotCode

Reputation: 125

You could try something like this too:

public class MainClass {

public static void main(String[] args) {
    Random bob = new Random();
    Scanner bill = new Scanner(System.in);

    String yesOrNo = "";
    do {

        int numToGuess = bob.nextInt(20);
        int numOfTries = 0;
        int guess = 0;
        boolean win = false;

        while (win == false) {

            System.out
                    .println("I'm thinking of a number between 1 and 20, can you guess it?");
            guess = Bill.nextInt();
            numOfTries++;

            if (guess == numToGuess) {
                win = true;
            } else if (guess < numToGuess) {
                System.out.println("Your guess is too low");
            } else if (guess > numToGuess) {
                System.out.println("Your guess is too high");
            }
        }

        System.out.println("Congratulations!");
        System.out.println("The number was " + NumToGuess);
        System.out.println("It took you " + NumOfTries + " tries");

        yesOrNo = bill.nextLine();
        System.out.println("Would you like to play again? " + yesOrNo);
        yesOrNo = bill.nextLine();

    } while (yesOrNo.equalsIgnoreCase("yes"));

    System.out.println("Ok, maybe next time.");
    System.exit(0);

}

}

Upvotes: 2

Avi
Avi

Reputation: 296

you can use labelled BREAK statement. Or you need to re - run the program as your while loops ends and you are out.

Upvotes: 0

Related Questions