You are going to write a program that allows the user to play a simple guessing
ID: 640260 • Letter: Y
Question
You are going to write a program that allows the user to play a simple guessing game in which your program thinks up an integer and allows the user to make guesses until the user gets it right. For each incorrect guess you will tell the user whether the right answer is higher or lower. Your program is required toexactlyreproduce the format and behavior of the log of execution at the end of this write-up.
At a minimum, your program should have the following static methods in addition to method main:
Explanation / Answer
import java.util.*;
public class NumberGuess3 {
static int totalGuesses;
static int totalGames;
static double average;
static int best = 9999;
public static void main(String[] args) {
giveIntro();
Scanner console = new Scanner(System.in);
char choice;
// pick a random number from 0 to 99 inclusive
Random rand = new Random();
do{ // iterates until user wants to quit
int number = rand.nextInt(100);
// get first guess
int guess = getGuess(console);
int numGuesses = 1;
// give hints until correct guess is reached
while (guess != number) {
int numMatches = matches(number, guess);
if(numMatches < 0){ // if numMatches is less than 0, then the number is lower
System.out.println("It's lower");
}
else if(numMatches > 0){ // if numMatches is greater than 0, then the number is higher
System.out.println("It's higher");
}
guess = getGuess(console);
numGuesses++;
}
totalGuesses += numGuesses; // adding guesses of game to totalGuesses for overall results
totalGames++; // incrementing games until user quits
System.out.println("You got it right in " + numGuesses + " tries.");
if(best > numGuesses) // defining best game with less number of guesses
best = numGuesses;
average = (double)(int)(((double)totalGuesses / totalGames) * 10) / 10;
System.out.print("Do you want to play again?" );
choice = console.next().charAt(0);
System.out.println();
}while(choice == 'y' || choice == 'Y');
results();
}
// method to give introduction
public static void giveIntro() {
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 right");
System.out.println("answer is higher or lower than your guess.");
System.out.println();
}
// Returns -1 if number is less than guess, 1 if number is greater, 0 if equal
public static int matches(int number, int guess) {
int numMatches = 0;
if(number > guess){
numMatches = 1;
}
else if(number < guess){
numMatches = -1;
}
return numMatches;
}
// Re-prompts until a number in the proper range is entered.
// post: guess is between 0 and 99
public static int getGuess(Scanner console) {
int guess = getInt(console, "Your guess? ");
while (guess < 0 || guess >= 100) {
System.out.println("Out of range; try again.");
guess = getInt(console, "Your guess? ");
}
return guess;
}
// Re-prompts until a number is entered.
public static int getInt(Scanner console, String prompt) {
System.out.print(prompt);
while (!console.hasNextInt()) {
console.next();
System.out.println("Not an integer; try again.");
System.out.print(prompt);
}
return console.nextInt();
}
// prints the overall results
public static void results()
{
System.out.println("Overall results:");
System.out.println("total games = " + totalGames);
System.out.println("total guesses = " + totalGuesses);
System.out.println("guesses/game = " + average);
System.out.println("best game = " + best);
}
}
Sample Run :
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.
Your guess? 50
It's higher
Your guess? 75
It's higher
Your guess? 90
It's higher
Your guess? 98
You got it right in 4 tries.
Do you want to play again?y
Your guess? 50
It's higher
Your guess? 75
It's lower
Your guess? 62
It's lower
Your guess? 55
It's higher
Your guess? 57
It's higher
Your guess? 59
It's higher
Your guess? 60
It's higher
Your guess? 61
You got it right in 8 tries.
Do you want to play again?y
Your guess? 50
It's lower
Your guess? 25
It's higher
Your guess? 37
It's higher
Your guess? 40
It's higher
Your guess? 45
You got it right in 5 tries.
Do you want to play again?n
Overall results:
total games = 3
total guesses = 17
guesses/game = 5.6
best game = 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.