Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a Java program called GuessNumber.java to make a game to guess a number

ID: 3638414 • Letter: D

Question

Develop a Java program called GuessNumber.java to make a game to guess a number between 1 and 100.

Operation:

• The application prompts the user to enter an int value from 1 to 100 until the user guesses the random number that the application has generated.

• The application displays messages that indicate whether the user’s guess is too high or too low.

• When the user guesses the number, the application displays the number of guesses along with a rating. Then, the application asks if the user wants to play again.

• When the user exits the game, the application displays a goodbye message.

 

Sample Run

Welcome to the Guess the Number Game
++++++++++++++++++++++++++++++++++++
I'm thinking of a number from 1 to 100.
Try to guess it.
Enter number: 50
You got it in 1 tries.
Great work!

Try again? (y/n): y

I'm thinking of a number from 1 to 100.
Try to guess it.
Enter number: 50
Too high! Guess again.

Enter number: 30
Too high! Guess again.

Enter number: 15
Too low! Guess again.

Enter number: 23
Too high! Guess again.

Enter number: 19
Too low! Guess again.

Enter number: 21
Too high! Guess again.

Enter number: 20
You got it in 7 tries.

Not too bad!

Try again? (y/n): Y
Error! Entry must be 'y' or 'n'. Try again.

Try again? (y/n): x
Error! Entry must be 'y' or 'n'. Try again.

Try again? (y/n): n
Bye - Come back soon!

Explanation / Answer

Hi, here is a java program that exactly follows your requirements and prints out the output the same way your sample does. I used the Math.random() function to generate a random number for the guessing game. I tried adding as many comments to the programm as I could without being too redundant or cumbersome, so give it a careful read and run it a couple of times and you should be able to udnerstand what goes on at each step. If there's something unclear or missing you can always send me a PM and we can look at it again.

I hope it helps, pelase remember to rate :)

import java.util.Scanner;

public class GuessNumber {

    public static void main(String[] args) {
       
        /* Math.random() gives a double precision
         * number between 0.0 and 1.0
         * so we multiply it by 99 to get
         * it in the 0 - 99 range, then
         * we add 1 to shift it to the 1-100 range
         * and get rid of the zero
         */
        int answer = (int)(Math.random()*99) + 1;
        /* userGuess is where we store the user's guess */
        int userGuess = 0;
        /* counter to keep track of the number of tries */
        int tries = 0;
       
        /* variable to store the user's choice to try again */
        char choice = 'y';
       
        /* input scanner to read the user input */
        Scanner scan = new Scanner(System.in);
       
        System.out.println("Welcome to the Guess the Number Game");
        System.out.println("+++++++++++++++++++++++++++++++++++++");
       
        while(choice != 'n' && choice !='N')
        {
            answer = (int)(Math.random()*99) + 1; /* new game - reset number */
            tries = 0; /* new game - reset tries */
           
            System.out.println("I'm thinking of a number from 1 to 100.");
            System.out.println("Try to guess it.");
           
            do{
                System.out.print(" Enter number: ");
                /* read user input as text and convert it to integer */
                userGuess = Integer.parseInt(scan.nextLine());
                /* increment number of tries made so far */
                tries = tries + 1;
               
                /* compare it to the answer */
                if(userGuess > answer) /* too high */
                {
                    System.out.println("Too high! Guess again.");
                }
                else if(userGuess < answer) /* too low */
                {
                    System.out.println("Too low! Guess again.");
                }
                else /* juuust right */
                {
                    System.out.println("You got it in " + tries + " tries.");
                    System.out.print(" ");
                    if(tries == 1) {
                        System.out.println("Great work!");
                    }
                    else {
                        System.out.println("Not too bad!");
                    }
                }
            }while(userGuess != answer);
           
           
            /* ask the user if they want to try again
             * while the choice is neither Y nor N
             * display an error message, then keep asking
             * the user if they want to try again
             * the loop will end once the user enters a valid choice
             */
            do {
                System.out.print(" Try again? (y/n): ");
                choice = (scan.nextLine()).charAt(0);
                /* */
                if( choice !='y' && choice!='Y' && choice !='n' && choice!='N')
                {
                    System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
                }
            }while(choice !='y' && choice!='Y' && choice !='n' && choice!='N');
        }/* end of while loop */
       
        System.out.println("Bye - Come back soon!");
       
    } /* end of main method */
} /* end of GuessNumber class */