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

Write a game program that can be played repeatedly. This game generates a random

ID: 3642191 • Letter: W

Question

Write a game program that can be played repeatedly. This game generates a random number between 1 and 100. The game prompts the user to guess the generated number. The game tells the user if he was low or high. The game allows the user to guess as many times as is it takes to guess the number. Allow the client to repeat the game as many times as she wishes. A sample run of the game would look like this:

This program allows you to guess a random integer
between 1 and 100 inclusive. The computer creates
a random number and tells you if your guess was
correct, hi or low.
Please guess the number
50 you were low, please guess again
75 you were high, please guess again
65 you were low, please guess again
70 you were low, please guess again
73 Good job! You got the number with 5 tries.

Would you like to play the game again?
Please enter (yes/no) no

Explanation / Answer

import java.util.Scanner;

public class GuessingNumberGame {
  
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String cont = "yes";
        int guess;
        int secret;
        int count;
      
        while (cont.equals("yes")) {
            System.out.println("This program allows you to guess a random integer");
            System.out.println("between 1 and 100 inclusive. The computer creates");
            System.out.println("a random number and tells you if your guess was");
            System.out.println("correct, hi or low. ");
          
            secret = (int)(Math.random() * 100) + 1;
          
            System.out.print(" Please guess the number ");
            guess = keyboard.nextInt();
            count = 1;
          
             while (guess != secret) {
                 System.out.print("You were "
                         + (guess > secret ? "high" : "low")
                         + ", please guess again ");
                 guess = keyboard.nextInt();
                 count++;
             }
             System.out.println("Good job! You got the number with " + count + " tries.");
           
             System.out.println(" Would you like to play the game again?");
             System.out.print("Please enter (yes/no) ");
             cont = keyboard.next();
             System.out.println();
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote