Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a number guessing game program named GuessingGame.java The program will generate a secret random number between 1 and 50 (inclusive). Use java.util.Random. The program

Write a number guessing game program named GuessingGame.java The program will generate a secret random number between 1 and 50 (inclusive). Use java.util.Random.

The program will continue prompting the user to guess the secret number until they guess it correctly. The program ends when the user successfully guesses the secret number.

Every time you prompt the user, use a while loop with the Scanner hasNextInt() method to make the program robust. If the user didn't enter an integer, discard their input, and reprompt with "Not an integer, guess again: ". If the entered value is outside the range (not from 1 - 50), reprompt with Out of range, guess again: ". The program should do the following:

Prompt the user for a guess (an integer value from 1 - 50, inclusive) While the guess does not equal the secret number: If the guess is larger than the generated random number, tell the user it is larger than the secret number and reprompt for another guess. If the guess its smaller than the generated random number, tell the user it is smaller than the secret number and reprompt for another guess.

Once the guess equals the secret number, tell the user that they guessed the secret number and they win! Here is a sample output from running your program:

Welcome to the Guessing Game. Try to guess the secret number between 1 and 50 (inclusive).

Your guess? 60 Out of range, guess again: z Not an integer, guess again: 30 30 is larger than the secret number

Your guess? 5 5 is smaller than the secret number

Your guess? 0 Out of range, guess again: 10 10 is smaller than the secret number

Your guess? 20 20 is larger than the secret number

Your guess? 15 15 is smaller than the secret number

Your guess? 18

You win! You guessed the secret number: 18

do not use break, try/catch.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions