Create the Number Guessing Game In this exercise, you’ll create a simple number
ID: 3888555 • Letter: C
Question
Create the Number Guessing Game In this exercise, you’ll 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:
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!
Create the game
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 don’t 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 user’s 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.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class Numberguessing
{
public static void main(String args[])
{
System.out.println("Welcome to the Number Guessing Game");
System.out.print("Enter the upper limit for the number:");
//cretae random number generator
Random rand = new Random();
Scanner s = new Scanner(System.in);
int a = Integer.parseInt(s.nextLine());
System.out.println("OK, I'm thinking of a number between 0 and "+a);
// use the random number generator
// pick a number 0 and upper limit
int num = rand.nextInt(a);
int nooftries = 0;
int guess = -1;
while(guess!=num)
{
System.out.print("Enter your Guess:");
// read the user guess
guess = s.nextInt();
nooftries++;
//checking for the guess
if (guess < num )
{
//guess is low
System.out.println("Your guess is too low.");
}else if (guess > num)
{
//guess is high
System.out.println("Your guess is too high.");
}else
{
//Guess is correct
System.out.println("Correct! " + "Bye! " + "The number was "+num);
System.out.println("It takes "+nooftries+" number of tries");
}
}
}
}
output:
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:45
Your guess is too high.
Enter your Guess:25
Your guess is too high.
Enter your Guess:5
Your guess is too low.
Enter your Guess:13
Your guess is too high.
Enter your Guess:9
Your guess is too high.
Enter your Guess:6
Correct!
Bye!
The number was 6
It takes 6 number of tries
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.