12.10Guess A Number* For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class...

1 answer below »

12.10Guess A Number*


For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project namedGuessANumberand create a new Java class in that project namedGuessANumber.javafor this assignment.


The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a message that is determined by how many guesses it takes them to get the right answer. If the user guesses incorrectly, the program should respond with a message that the user has guessed either "too high" or "too low" and let them guess again.


Use the following table to determine the final message after the score is computed (note that your code MUST respond with these messages to be considered correct):



Number of Guesses Message ----------------- --------------------------------------------- 1 That was impossible! 2-3 You're pretty lucky! 4-7 Not bad, not bad... 8 That was not very impressive. 9-10 Are you having any fun at all? 11 or more Maybe you should play something else.


Sample OutputThis is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.


Enter a random seed: 99 Enter a guess between 1 and 200: 100 Your guess was too low - try again.  Enter a guess between 1 and 200: 150 Your guess was too low - try again.  Enter a guess between 1 and 200: 175 Your guess was too low - try again.  Enter a guess between 1 and 200: 187 Your guess was too low - try again.  Enter a guess between 1 and 200: 193 Your guess was too high - try again.  Enter a guess between 1 and 200: 190 Your guess was too high - try again.  Enter a guess between 1 and 200: 188 Congratulations!  Your guess was correct!  I had chosen 188 as the target number. You guessed it in 7 tries. Not bad, not bad...

Your code will behave differently based on the random value it selects and the choice taken by the user. Here is a second possible execution of this code:


Enter a random seed: 1024 Enter a guess between 1 and 200: 120 Your guess was too low - try again.  Enter a guess between 1 and 200: 130 Your guess was too high - try again.  Enter a guess between 1 and 200: 124 Congratulations!  Your guess was correct!  I had chosen 124 as the target number. You guessed it in 3 tries. You're pretty lucky!

If the user enters an invalid choice, your code should inform them that their choice was invalid and ask them to guess again.


Enter a random seed: 99 Enter a guess between 1 and 200: 259 Your guess is out of range.  Pick a number between 1 and 200. Your guess was too high - try again.  Enter a guess between 1 and 200: -1 Your guess is out of range.  Pick a number between 1 and 200. Your guess was too low - try again.  Enter a guess between 1 and 200: 188 Congratulations!  Your guess was correct!  I had chosen 188 as the target number. You guessed it in 3 tries. You're pretty lucky!

(Note that since the user picked the same random number seed as the first example, the program has selected the same value as the target number.)


Random numbers:Just like in the FunWithBranching programming assignment, you must use theRandomclass to generate numbers between 1 and 200. Make sure you are usingintvalues and notdoublesfor this assignment!



Answered Same DaySep 27, 2021

Answer To: 12.10Guess A Number* For this assignment you will write a Java program using a loop that will play a...

Neha answered on Sep 27 2021
143 Votes
import java.util.*;
public class GuessANumber
{
    public static void main(String[] args) {
     S
canner in= new Scanner(System.in);
     int answer, guess, count = 0;
     final int MAX = 200;
     Random rand = new Random();
boolean correct = false;
answer = rand.nextInt(MAX) + 1;
System.out.println("Enter a random seed: ");
int seed = in.nextInt();
while (!correct) {

System.out.println("Guess a number between 1 and 200: ");
guess...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here