Need help with this simple java programing for my class making this game with pa
ID: 3860883 • Letter: N
Question
Need help with this simple java programing for my class making this game with parts 1 and 2 if possible, also this is intro class so please simple language is best for me to understand.
Write a program that plays a reverse guessing game with the user. The user thinks of a number between 1 and 10 and the computer repeatedly tries to guess it by selecting random numbers. It is ok for your first solution to guess the same number more than once. At the end of the game the computer should report the number of guesses it made.
Example:
This program has you, the user, choose a number
between 1 and 10. The computer will try 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 in 7 guesses.
Once you have your solution you can optionally add three features to the program to make it more fun to use.
1. Change the program to be able to ask the user for the first and last numbers for the random range at the beginning of the program. The random numbers and the number the user selects will be in this range.
2. Change the program so that the computer no longer guesses numbers it has already guessed. You may want to use an array or another data structure to help with this.
Explanation / Answer
PROGRAM CODE:
package guess;
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
static int guesses[];
static int counter;
static Random rand;
//checks if the number guessed is already guessed or not. If not, then its
//adds the number ot the list of guesses
public static boolean isGuessed(int guess)
{
for(int i=0; i<counter; i++)
{
if(guesses[i] == guess)
return true;
}
guesses[counter++] = guess;
return false;
}
//generates a new random guess
public static int generateGuess(int min, int max)
{
return rand.nextInt(max-min)+min;
}
public static void main(String[] args) {
rand = new Random();
int start, end, guess;
char choice;
counter = 0;
guesses = new int[100];
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the first number in range: ");
start = keyboard.nextInt();
System.out.print(" Enter the last number in range: ");
end = keyboard.nextInt();
System.out.println(" This program has you, the user, choose a number");
System.out.println("between " + start + " and " + end + ". The computer will try to guess it.");
while(true)
{
//until the guess is a new number the loop runs
do
{
guess = generateGuess(start, end);
}while(isGuessed(guess));
System.out.print(" Is it " + guess + "? (y/n) ");
choice = keyboard.next().charAt(0);
if(choice == 'y')
{
System.out.println(" I got your number of " + guess + " in " + counter + " guesses.");
System.exit(0);
}
}
}
}
OUTPUT:
Enter the first number in range: 1
Enter the last number in range: 20
This program has you, the user, choose a number
between 1 and 20. The computer will try to guess it.
Is it 6? (y/n) n
Is it 11? (y/n) y
I got your number of 11 in 2 guesses.