write a program that plays a reverse guessing game with the user.the user think
ID: 3623505 • Letter: W
Question
write a program that plays a reverse guessing game with the user.the user think of a number between 1 and 10,and the computer repeatedly tries to guess it by guessing random numbers.it's fine for, the computer to guess the same random number more than ones.at the end of the game,the program reports how many guesses it made.here is a sample execution:
This program has you the user,choose a number between 1 and 10.then computer,will try my best to guess it.
is it 8 (y/n) n
is it 7 (y/n) n
is it 5 (y/n) n
is it 1 (y/n) n
is it 8 (y/n) n
is it 1 (y/n) n
is it 9 (y/n) y
I got your number of 9 correct in 7 guesses.
Explanation / Answer
alright so first you need to get the basic stuff down first like variables
public class problem
{ public static void main(String[] args)
{ int max=10, min=1, counter=0, numguess=0, guess=10;
boolean a = false;
next you'll need to import something that can read ints and booleans because thats what the user will be inputing. I use this scanner most of the time.
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
after that we actually need to take the number that the user is inputing so we use the new scanner that we imported like this. You should also give a prompt so that the user knows what to input
System.out.println("Whats the number? Between 1 and 10");
int mynum = scan.nextInt();
now comes the fun part. We will need to make a loop with if/else statements to check the number against all the possible guesses. I will use a do/while with if/elses. you can use a different loop if you want.
do
{ guess= (guess-counter);
numguess= (numguess+1);
System.out.println("Is your number "+guess+"?");
a = scan.nextBoolean();
if (a)
{ System.out.println("It took "+numguess+"guesses."); }
else
{ counter = (counter+1); }
}
while (!(a))
There is a lot of meat in that loop so let me break it down. the first line uses the guess variable to check all possible numbers the user's guess can be. The next line stores the number of guesses the program has taken. the next 2 lines are to ask if the guess is the correct one. The if/else is the most important line of the code. If the guess is true the program will print the number of guesses take and if the guess is false it increases counter by 1 and goes through the loop again.
Here is the full code without interuption
public class problem
{ public static void main(String[] args)
{ int max=10, min=1, counter=0, numguess=0, guess=10;
boolean a = false;
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
System.out.println("Whats the number?");
int mynum = scan.nextInt();
do
{ guess= (guess-counter);
numguess= (numguess+1);
System.out.println("Is your number "+guess+"?");
a = scan.nextBoolean();
if (a)
{ System.out.println("It took "+numguess+"guesses."); }
else
{ counter = (counter+1); }
}
while (!(a))
}
}
I have not compiled or ran this code because I am not home right now (at a comp lab) so there may be bugs in there that I cant see without compiling. If there is a bug just comment and when i get home I will see whats up
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.