Hello Guys,

I'm having trouble getting my code to run correctly. I need the program to ask the user do they want to continue after they guess the number correctly. If they say "y" the loop should restart if they reply "n" the program should end and close. I will post my code I have so far,any suggestions are appreciated. Thanks in advance.

 public static void main(String[] args) {
    // Welcome user to program
    System.out.println("Welcome to the Random Number Game!\n");

    // Create Scanner Object
    Scanner sc = new Scanner(System.in);
    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {

        // Get Random Double Number
        int number_of_tries = 0;
        double randNumber = Math.random();
        double d = randNumber * 100;
        int randomNum = (int)d + 1;

        // Beginning Game Message
        System.out.println("I'm thinking of a number between 1 - 100.");
        System.out.println("Can you guess it?");

        // Obtain User Guesses
            System.out.println("Let''s Play!\n");
            number_of_tries++;

            for(int i = 1; i <= 7; i++) {

                int userInt = getIntWithinRange(sc, "Enter your guess: ", 1, 100);

                if (userInt > randomNum + 10) 
                    System.out.println("Way too high!");

                else if (userInt < randomNum - 10) 
                    System.out.println("Way too low!");

                else if (userInt > randomNum)
                    System.out.println("Too high!");

                else if (userInt < randomNum)
                    System.out.println("Too low!");

                else 
                    System.out.println("You guessed right!");


         // End For Loop


    // See if user wants to play again          
choice = "x";
while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
        System.out.println("Attempt number " + number_of_tries );    
        System.out.println("Do you wish to play again? (y/n): ");
        choice = sc.next();
        sc.nextLine();

            if (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
                System.out.println("Error! Not a valid responce!");
            } // End if Loop.

        } // End While Choice Loop.

    } // End While Loop.

    } // End Main.
    }

    public static int getIntWithinRange(Scanner sc, String prompt, int min, int max) {
    int number = 0;
        boolean isValid = false;
        while (isValid == false) {
            number = getInt(sc, prompt);
            if (number <= min)
                System.out.println("Error! Number must be greater than " + min + ".");
            else if (number >= max)
                System.out.println("Error! Number must be less than " + max + ".");
            else
                isValid = true;

        }// End While Loop
        return number;

    } // End Rage Checker


    public static int getInt (Scanner sc, String prompt) {
        int number = 0;
        boolean isValid = false;
        while (isValid == false) {
            System.out.print(prompt);

            if (sc.hasNextInt()) {
                number = sc.nextInt();
                isValid = true;
            } // End If

            else {
                System.out.println("Error! Invalid integer value.  Try again.");
            } // End Else

            sc.nextLine();      
        } // End While Loop
        return number;

    }// End Integer Checker

} // End Class.

The thing that immediately jumps out is that on line 44 you comment end of for loop, but theres no closing bracket to implement that, so the for loop is not going to end where you think, and subsequesnt loops and blocks will also be suspect.
If you are using an IDE or Editor that does automatic indenting that will make problems like this very obvious.
Like this:

       // Welcome user to program
        System.out.println("Welcome to the Random Number Game!\n");
        // Create Scanner Object
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
            // Get Random Double Number
            int number_of_tries = 0;
            double randNumber = Math.random();
            double d = randNumber * 100;
            int randomNum = (int) d + 1;
            // Beginning Game Message
            System.out.println("I'm thinking of a number between 1 - 100.");
            System.out.println("Can you guess it?");
            // Obtain User Guesses
            System.out.println("Let''s Play!\n");
            number_of_tries++;
            for (int i = 1; i <= 7; i++) {
                int userInt = getIntWithinRange(sc, "Enter your guess: ", 1, 100);
                if (userInt > randomNum + 10) {
                    System.out.println("Way too high!");
                } else if (userInt < randomNum - 10) {
                    System.out.println("Way too low!");
                } else if (userInt > randomNum) {
                    System.out.println("Too high!");
                } else if (userInt < randomNum) {
                    System.out.println("Too low!");
                } else {
                    System.out.println("You guessed right!");
                }
         // End For Loop
                // See if user wants to play again          
                choice = "x";
                while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
                    System.out.println("Attempt number " + number_of_tries);
                    System.out.println("Do you wish to play again? (y/n): ");
                    choice = sc.next();
                    sc.nextLine();
                    if (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
                        System.out.println("Error! Not a valid responce!");
                    } // End if Loop.
                } // End While Choice Loop.
            } // End While Loop.
        } // End Main.
    }

Edited 1 Year Ago by JamesCherrill

Thanks, I appreciate your help! After changing the brackets I still have an issue now when the program runs and the user gets it correct, it asks them do they want to play again, but also tells thems to enter a number automatically as well. Is there anything else I'm overlooking.[

public class GuessingGamesApp {

    //Int within range validation method
    public static int getIntWithinRange(Scanner sc, String prompt, int min, int max) {
        int number = 0;
        boolean isValid = false;
        while (isValid == false) {
            number = getInt(sc, prompt);
            if (number <= min) {
                System.out.println("Error! Number must be greater than " + min + ".");
            } else if (number >= max) {
                System.out.println("Error! Number must be less than " + max + ".");
            } else {
                isValid = true;
            }

        }
        return number;
    } 

    //validation method checking if value is an integer
    public static int getInt(Scanner sc, String prompt) {
        int number = 0;
        boolean isValid = false;
        while (isValid == false) {
            System.out.print(prompt);

            if (sc.hasNextInt()) {
                number = sc.nextInt();
                isValid = true;
            } 
            else {
                System.out.println("Error! Entry must be an integer value! Please try again.");
            } 

            sc.nextLine();
        } 
        return number;
    }

    public static void main(String[] args) {

        // Welcome user to program
        System.out.println(" Welcome to the Guess the Number Game! ");
        System.out.println(" +++++++++++++++++++++++++++++++++++++ \n");


// Create Scanner Object
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
// Get Random Double Number
            int number_of_tries = 0;
            double randNumber = Math.random();
            double d = randNumber * 100;
            int randomNum = (int) d + 1;

// Beginning Game Message
            System.out.println(" I'm thinking of a number between 1 & 100. ");
            System.out.println(" Try to guess it! ");
// Obtain User Guesses
            System.out.println("Let''s Play!\n");
            number_of_tries++;
            for (int i = 1; i <= 7; i++) {
                int userInt = getIntWithinRange(sc, "Enter your guess: ", 1, 100);
                if (userInt > randomNum + 10) {
                    System.out.println("Way too high!");
                    number_of_tries++;
                } else if (userInt < randomNum - 10) {
                    System.out.println("Way too low!");
                    number_of_tries++;
                } else if (userInt > randomNum) {
                    System.out.println("Too high!");
                    number_of_tries++;
                } else if (userInt < randomNum) {
                    System.out.println("Too low!");
                    number_of_tries++;
                } else {
                    System.out.println("Ah ha! You got it!!!");
                    System.out.println(" It took you " + number_of_tries + "tries!" );
                    System.out.println("Do you wish to play again? (y/n): ");
                }

            }
            choice = "x";
            while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
                System.out.println("Attempt number " + number_of_tries);
                System.out.println("Do you wish to play again? (y/n): ");
                choice = sc.next();
                sc.nextLine();
                if (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
                    System.out.println("Error! Not a valid responce!");
                } 
            } 
        } 
    }
}

](null)

Line 64 is where the loop in question starts, so that would be a bad place to break.
Your loop runs 7 times, even if the user guesses correctly. YOU need to stop the loop when the user guiesses correctly. a break is one way to exit a loop. YOU have to work out how to update your own code.

This article has been dead for over six months. Start a new discussion instead.