0
votes

I've just started learning programming for the first time and I am working through Java to start. I am doing a common coding exercise of programming a guessing game using loops and conditionals. My program is required to do the following:

  1. Pick a random number between 1 and 100
  2. Repeatedly prompt the user to guess the number
  3. Report to the user that he or she is correct or that the guess is high or low after each guess
  4. Offer the user the option to quit mid-game
  5. Count the number of guesses in a game and report the number upon a correct guess
  6. Ask the user if they want to play again upon a successful game

I have been a little bit shaky with loop syntax so far and need some help with my program because there are a lot of issues I don't know how to fix. Would anyone be kind enough to lend me a hand? Please forgive the many probably obvious mistakes.


import java.util.Scanner;

import java.util.Random;

public class Guess

{

public static void main (String[] args)

{

    final int MAX = 100;
    int answer, guess = 0, count = 0;
    String another = "y";

    Random generator = new Random();
    answer = generator.nextInt(MAX) + 1;

    Scanner scan = new Scanner(System.in);

    System.out.println("I'm thinking of a number between 1 and " + MAX
                        + ". Guess what it is: ");
    guess = scan.nextInt();

    while (another.equalsIgnoreCase("y"))
    {         
        while (guess != answer)
        {

            while (guess > MAX || guess < 1)
            {
                System.out.println("Invalid input. Please re-enter a number"
                + " between 1 and " + MAX + ":");
                guess = scan.nextInt();
            }

            if (guess == answer)
            {      
            count++;
            System.out.println("You guessed correctly!");
            }
            else if (guess > answer)
            {
                count++;
                System.out.println("You guessed too high. Guess again? Y/N:");
                another = scan.nextLine();
            }
            else if (guess < answer)
            {
                count++;
                System.out.println("You guessed too low. Guess again? Y/N:");
                another = scan.nextLine();
            }
        }
    }

    System.out.println("It took you " + count + "guess(es) to win.");
    System.out.println("Do you wish to play again? Y/N:?");
    another = scan.nextLine();
    count = 0;
    answer  = generator.nextInt(MAX) + 1;
}

}

1

1 Answers

0
votes

One problem is that you're not letting the user quit midway through the game because even if the user guesses a number within the 1 to 100 range and doesn't get the right answer, his or her answer to Guess again: Y/N: won't be checked since the current loop it is in only compares guess to answer, never another. Therefore, you'll end up being in an infinite loop in this case because if the user guesses 57 when the answer is 50, you'll just continuously prompt the user if he or she wants to guess again.

My recommendation would be to remove the second while loop

while (guess != answer)
{
    //other stuff
}

and place the code inside that loop into the outside while loop

while(another.equalsIgnoreCase("y")){
    //other stuff
}

And if you want the user to be able to play again, I would recommend putting this snippet of code you had earlier inside the if statement where you check if the user has guessed correctly,

if (guess == answer)
{      
    count++;
    System.out.println("You guessed correctly!");
    System.out.println("It took you " + count + "guess(es) to win.");
    System.out.println("Do you wish to play again? Y/N:?");
    another = scan.nextLine();
    count = 0;
    answer  = generator.nextInt(MAX) + 1;
}

This way, if the user wins the game, their choice to play again will be checked in the while loop. One last thing I would recommend is moving this line

guess = scan.nextInt();

inside the while loop that checks another so that if the user wants to play again, the game will prompt the user for a guess.