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

write a guessing game in which the computer picks a random integer between 1 and

ID: 3671285 • Letter: W

Question

write a guessing game in which the computer picks a random integer between 1 and 100 (inclusive), and you have to guess what it is. the game should follow these guidelines:

1. include error checking to ensure that player cant enter something below 1 or above 100.

2. the player has maximum of 5 tries to correctly guess the number . if cant get it after 5 he/she loses the game. note that invalid input(something below 1 or above 100) should not count against the number of tries

3. after each valid guess, display a message saying whether the guess was too high, too low or correct. also display the number of guesses remaining.

4. if player is able to correctly guess the number, display a message saying how many tries it took to do so

5. if the player is unable to correctly guess the number after 5 tries display a message saying what the correct number was.

6. allow the player to repeat the game as many times as he/she wishes

Explanation / Answer

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

public class Guess {
    /**
     * The random number to guess.
     */
    private static int numToGuess;
    /**
     * The user entered guess.
     */
    private static int guess;
    /**
     * The count of guesses.
     */
    private static int guesses;
    /**
     * <p>Scans the user guess and compares it to a randomly generated
     * number between 1 and 100.</p>
     *
     * @param args command line arguments.
     */
    public static void main(String[] args) {
      
        String tryAgain = new String("Y"); //The continue value
        final int quit = 0;                //The quit value
        final int max = 100;               //The max number to generate
      
        Scanner scan = new Scanner(System.in);
        Random generator = new Random();
      
        while (tryAgain.equalsIgnoreCase("Y")) {
            guesses = 0;
            numToGuess = generator.nextInt(max) + 1;
          
            System.out.println("Guess a number between 1 and 100 [0 to quit]:");
            guess = scan.nextInt();
            guesses++;
                                  
            while (guess != numToGuess && guess != quit) {
                guessing();
            }
            if (guess == numToGuess) {
                System.out.println("That is correct! You took " + guesses
                        + " guesses");
            } else if (guess == 0) { //Game ends
                System.out.println("You have ended your game. Goodbye.");
            }
                                
            System.out.println("Would you like to try again Y/N?");
            tryAgain = scan.next();
        }
      
        if (guess != numToGuess) {
            System.out.println("You quit after " + guesses + " guesses and "
                    + " the number was " + numToGuess);
        }
    }
          
    /**
     * <p> This method compares the guess to the generated number and lets
     * the user know if they are guessing higher or lower than the number.
     * It prompts for another guess and counts the amount of tries.</p>
     */
    private static void guessing() {
        Scanner scan = new Scanner(System.in);
        if (guess < numToGuess) {
            System.out.println("The number is too low, try again");
        }
        if (guess > numToGuess) {
            System.out.println("The number is too high, try again");
        }
        guess = scan.nextInt();
        guesses++;
    }
}

output

Guess a number between 1 and 100 [0 to quit]:                                                                                                               
5                                                                                                                                                           
The number is too low, try again                                                                                                                            
20                                                                                                                                                          
The number is too low, try again                                                                                                                            
55                                                                                                                                                          
The number is too low, try again                                                                                                                            
75                                                                                                                                                          
The number is too low, try again                                                                                                                            
95                                                                                                                                                          
The number is too high, try again                                                                                                                           
85                                                                                                                                                          
The number is too high, try again                                                                                                                           
80                                                                                                                                                          
The number is too high, try again                                                                                                                           

The number is too low, try again                                                                                                                            
55                                                                                                                                                          
The number is too low, try again                                                                                                                            
75                                                                                                                                                          
The number is too low, try again                                                                                                                            
95                                                                                                                                                          
The number is too high, try again                                                                                                                           
85                                                                                                                                                          
The number is too high, try again                                                                                                                           
80                                                                                                                                                          
The number is too high, try again                                                                                                                           
77                                                                                                                                                          
That is correct! You took 8 guesses                                                                                                                         
Would you like to try again Y/N?                                                                                                                            
n