Write a program that plays a guessing game with the user. The program should gen
ID: 3645601 • Letter: W
Question
Write a program that plays a guessing game with the user. The program should generate arandom number between 1 and some maximum (such as 100), then prompt the user repeatedly to
guess the number. When the user gives a wrong guess then computer should hint the user if the
correct answer is higher or lower than the guess. Once the user guesses it correctly, then your
program should print a message indicating the number of guesses.
Here is a sample output:
This program allows you to play a guessing game.
I will think of a number between 1 and 100
and will allow you to guess until you get it.
For each guess, I will tell you whether the
right answer is higher or lower than your guess.
I'm thinking of a number...
Your guess? 50
lower
Your guess? 25
lower
Your guess? 12
You got it right in 3 guesses
Do you want to play again? yes
I'm thinking of a number...
Your guess? 120
lower
Your guess? 100
lower
Your guess? 50
lower
Your guess? 25
lower
Your guess? 12
lower
Your guess? 6
lower
Your guess? 3
lower
Your guess? 2
You got it right in 8 guesses
I
Do you want to play again? yes
I'm thinking of a number...
Your guess? 80
lower
Your guess? 40
lower
Your guess? 20
higher
Your guess? 30
higher
Your guess? 35
higher
Your guess? 38
lower
Your guess? 37
lower
Your guess? 36
you got it right in 8 guesses
Do you want to play another game?
yes
I'm thinking of a number...
Your guess? 50
lower
Your guess? 25
higher
Your guess? 35
higher
Your guess? 45
lower
Your guess? 40
lower
Your guess? 35
higher
Your guess? 38
lower
Your guess? 37
Iyou got it right in 8 guesses
Do you want to play another game?
no
Explanation / Answer
please rate - thanks
import java.util.*;
public class GuessingGame
{
public static void main(String [] args)
{Scanner in=new Scanner(System.in);
Random r = new Random();
int num;
int guess,secret;
int guesses;
String reply;
System.out.println("This program allows you to play a guessing game.");
System.out.println("I will think of a number between 1 and 100");
System.out.println("and will allow you to guess until you get it.");
System.out.println("For each guess, I will tell you whether the");
System.out.println("right answer is higher or lower than your guess. ");
do
{guess=0;
guesses=0;
secret =r.nextInt(100)+1;
System.out.println("I'm thinking of a number...");
System.out.print("Your guess? ");
guess=in.nextInt();
guesses++;
while(guess!=secret)
{guesses++;
if(guess>secret)
System.out.println("lower");
else if(guess<secret)
System.out.println("higher");
System.out.print("Your Guess? ");
guess=in.nextInt();
}
System.out.println("You got it right in "+ guesses + " guesses");
System.out.print("Do you want to play again? ");
in.nextLine();
reply=in.nextLine();
}while(reply.compareToIgnoreCase("yes")==0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.