Question: The following lab is to be coded using JAVA on JGRASP: Consider the game Guess the Number. Player 1 says, I'm thinking of a number

The following lab is to be coded using JAVA on JGRASP:

Consider the game "Guess the Number." Player 1 says, "I'm thinking of a number between 1 and 100." Player 2 says, "50." Player 1 replies, "No, the number is less." Player 2 says, "25," and so on, until she guesses correctly.

Write a program that plays this game. The computer generates a random number between 1 and 100, and the user guesses that number. At the end of the game, the computer displays the number of guesses required by the user to guess the number correctly.

You must validate the inputs in this lab: do not let the user enter a number outside of the range of 1 to 100. Numbers out of range do not count as guesses.

Example Program Run:

I'm thinking of a number between 1 and 100:

Your guess?

300

Please enter a number in range:

Your guess?

-33

Please enter a number in range:

Your guess?

50

Incorrect - guess a larger number

Your guess?

75

Incorrect - guess a larger number

Your guess?

88

Incorrect - guess a smaller number

Your guess?

82

Incorrect - guess a larger number

Your guess?

101

Please enter a number in range:

Your guess?

85

Incorrect - guess a smaller number

Your guess?

84

Incorrect - guess a smaller number

Your guess?

83

Correct! It took you 7 tries!

Here is the following code I have so far:

import java.util.Scanner; public class GuessingGame { public static void main (String[] args) { Scanner scanner= new Scanner(System.in); System.out.println("I'm thinking of a number between 1 and 100. Your guess? "); int x = scanner.nextInt(); if ((x<=1)||(x>=100)) { System.out.println("Please enter a number that is within range"); } while (x<=70) { System.out.println("No the number is greater. Keep guessing."); } if (x>=70) { System.out.println("You are close. Keep guessing greater."); } if (x>=80) { System.out.println("Guess a number less."); } if (x==75) { System.out.println("Congratulations, you've guessed the number!"); } } }

First, the code is missing pieces, and I need help creating the game perfectly. Also, I know that the if statements only allow for the user to enter one number, and I need them to be able to enter multiple numbers for their guesses. Can someone please help me with my code?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!