The purpose of the code is to play a number guessing game with the user. The current
Question:
The purpose of the code is to play a number guessing game with the user. The current output of this code is the following: Let's play a number guessing game. How many games do you want to play? 3 Play with a range of 50 or 100 or 150? 56 Retry! Please enter 50 or 100 or 150 Do you want to play with a range of 50 or 100 or 150? 100 I'm thinking of a number... Your guess? My goal is to modify it, so that where it states "Play with a range of 50, 100, or 150?" it reads instead "For these (player's chosen # of games) games, what is the range of numbers we are going to use? 1 to __?"
100 (user's input: 50, 100, or 150) I have attached a screenshot of the current output below. To clarify, my objective is to get the user's selected number of games to appear in the printed statement asking the range they wish to play with."For these (player's chosen # of games between 3 and 5) games, what is the range of numbers we are going to use? 1 to __?" 100 (user's input: 50, 100, or 150) Any help would be sincerely appreciated. Thank you very much in advance for your time!
Code: import java.util.*;
import java.util.Scanner;
public class Guess1
{
public static final int max = 100;
public static void main(String[] args)
{
int gameCount = 0;
int guessCount = 0;
int totalGuesses = 0;
int maxGuess = 0;
giveIntro();
int numberOfGames = getNumberOfGames();
for (int i=0;i
{
guessCount = playGame();
totalGuesses = totalGuesses + guessCount;
maxGuess = Math.max(maxGuess, guessCount);
gameCount++;
}
System.out.println();
results(totalGuesses, gameCount, maxGuess);
}
public static void giveIntro()
{
System.out.println("Let's play a number guessing game.");
}
public static int getNumberOfGames()
{
int minGames = 3;
int maxGames = 5;
System.out.println("How many games do you want to play?");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
while (number < minGames || number > maxGames)
{
System.out.println("I'm sorry. We must play between 3 and 5 games.");
System.out.println("How many games do you want to play?");
number = scanner.nextInt();
}
return number;
}
public static int askPlayer(){
Scanner sc = new Scanner(System.in);
System.out.println("Play with a range of 50 or 100 or 150?");
int range = sc.nextInt();
while(range!=50 && range!=100 && range!=150){
System.out.println("Retry! Please enter 50 or 100 or 150");
System.out.println("Do you want to play with a range of 50 or 100 or 150?");
range = sc.nextInt();
}
return range;
}
public static int playGame()
{
int rangeOfNumbers = askPlayer();
Scanner console = new Scanner(System.in);
Random r = new Random(rangeOfNumbers);
int answer = r.nextInt(max) + 1;
int guess = 0;
int guessCount = 1;
System.out.println("I'm thinking of a number...");
System.out.print("Your guess?");
guess = console.nextInt();
while (guess != answer)
{
if (guess < answer)
{
System.out.println("higher");
}
else
{
System.out.println("lower");
}
System.out.print("Your Guess?");
guess = console.nextInt();
guessCount++;
}
System.out.println("You got it right in " + guessCount + " guesses.");
return guessCount;
}
public static void results(int guesses, int games, int max)
{
double avgGuess = round2((double) guesses / (double) games);
System.out.println("Overall results:");
System.out.println("total games = " + games);
System.out.println("total guesses = " + guesses);
if (avgGuess == Math.round(avgGuess))
{
System.out.println("guesses/game = " + (int) avgGuess);
}
else
{
System.out.println("guesses/game = " + avgGuess);
}
System.out.println("max guesses = " + max);
}
public static double round2(double number)
{
return Math.round(number * 100.0) / 100.0;
}
}
1014 Practice Questions For The New GRE
ISBN: 9780375429682
2nd Edition
Authors: The Princeton Review