This question will give you practice with while loops and pseudorandom numbers.
ID: 3774270 • Letter: T
Question
This question will give you practice with while loops and pseudorandom numbers. You are going to write a program that allows the computer to play a simple guessing game in which you think an integer and allows the computer to make guesses until the computer gets it right. For each incorrect guess you will tell the computer whether the right answer is h for higher or l for lower. At a minimum, your program should have the following static methods in addition to method main:
a method that introduces the game to the computer
a method to play one game with the computer
You may define more methods than this if you find it helpful, although you will find that the limitation that methods can return only one value will tend to limit how much you can decompose this problem. You are to define a class constant for the maximum number used in the guessing game. The sample log shows the user making guesses from 1 to 100, but the choice of 100 is arbitrary. By introducing a constant for 100, you should be able to change just the value of the constant to make the program play the game with a range of 1 to 50 or a range of 1 to 250 or some other range starting with 1. When you ask the user whether or not to play again, you should use the “next()” method of the Scanner class to read a one-word answer from the user. You should continue playing if this answer begins with the letter “y” or the letter “Y”. Notice that the user is allowed to type words like “yes”. You are to look just at the first letter of the user’s response and see whether it begins with a “y” or “n” (either capitalized or not) to determine whether to play again.
Example (Blod is user's input.)
This program allows computer to play a guessing game. I will think of a number between 1 and 100 and will allow computer to guess until computer get it. For each guess, I will tell computer whether the right answer is higher (=h) or lower (=l) than computer’s guess.
I'm thinking of a number between 1 and 100...
My guess: 50
l
My guess: 25
l
My guess: 12
l
My guess: 6
correct
Do you want to play again? Y
I'm thinking of a number between 1 and 100...
My guess: 50
l
My guess: 25
l
My guess: 12
h
My guess: 18
correct
Do you want to play again? No
Explanation / Answer
//used binary search to guess the number so my guess will never be repeated
import java.util.Random;
import java.util.Scanner;
public class Guess {
static final int range = 100;
static int[] list = new int[range];
static Scanner in;
static int low=0,high=100;
public static void main(String[] args) {
in = new Scanner(System.in);
initialize();
System.out.println("Do you want to Play the game?");
String s = in.next();
while (("" + s.charAt(0)).equalsIgnoreCase("y")) {
System.out.println("I'm thinking of a number between 1 and 100...");
Random r = new Random();
int n = r.nextInt(100);
System.out.println(n);
guess_answer(111);
System.out.println("Do you want to play again?");
s = in.next();
}
System.out.println("Thank you!");
}
static void guess_answer(int n) {
int g = binarySearch();
String s = "";
while (!s.equalsIgnoreCase("correct")) {
System.out.println("My guess: " + g);
s = in.next();
if (s.equalsIgnoreCase("h"))
{
low=g;
g = binarySearch();
}
if (s.equalsIgnoreCase("l"))
{
high=g-1;
g = binarySearch();
}
if (s.equalsIgnoreCase("correct")) {
System.out.println("Correct");
s = "correct";
low=0;
high=100;
}
}
}
static void initialize() {
for (int i = 0; i < range; i++) {
list[i] = i + 1;
}
}
public static int binarySearch() {
System.out.println("low is "+low+" high is "+high);
int middle = (low + high-1) / 2;
return list[middle];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.