Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some help on a Java number guessing game. In this exercise, youll create a simple number guessing game that allows you to guess the

Need some help on a Java number guessing game.

In this exercise, youll create a simple number guessing game that allows you to guess the value of a random number. Each time you make a guess, the game tells you whether your guess is too high or too low. This process repeats until you guess the correct number. A sample run of the application should look like this:

image text in transcribed

1. Start NetBeans and open the project named ch03_ex3_GuessingGame in the extra_ex_starts directory.

2. Review the existing code. Note the use of the Random class. This code generates a random integer between 0 and upperLimit. For the purposes of this exercise, you dont need to understand how it works.

3. Write the code that prompts the user for an upper limit for the guess. This code should set the upperLimit variable which is already defined.

4. Write the code that prompts the user for their first guess.

5. Write the while loop that allows the user to guess again if their guess was wrong. This loop should inform the user whether their guess was too high or too low. Then, it should prompt the user for a new guess. The loop should exit when the users guess is equal to the random number the computer generated.

6. After the while loop, write code that tells the user they guessed correctly.

7. Run the application and make sure it works correctly.

Enhance the game

8. Before the while loop, add a variable to keep track of the number of guesses the user has made. Remember to initialize this variable to 1 instead of 0 since the user will need at least one guess to get the correct number.

9. Inside the while loop, increment the variable by one each time the user guesses incorrectly.

10. After the while loop, add a message that tells the user how many guesses they took.

This is the code already there:

image text in transcribed

package murach.games;

import java.util.Random;

public class Main {

public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); // TODO: Replace next line with code that prompts user for upper limit. int upperLimit = 10;

// Generate a random number between 0 and the upperLimit variable Random random = new Random(); int number = random.nextInt(upperLimit - 1) + 1; System.out.println("number: " + number); // remove this from final app } }

Thanks so much in advance. Needs this as soon as possible.

Welcome to the Number Guessing Game Enter the upper limit for the number: 50 OK, I'm thinking of a number between 0 and 50 Enter your guess: 25 Your guess is too high. Enter your guess: 20 Your guess is too low. Enter your guess: 23 Correct! Bye

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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