Question
Having a bit of trouble completing part of the code for my guessing game in java. Basically, I need to add the part of the
Having a bit of trouble completing part of the code for my guessing game in java. Basically, I need to add the part of the code that will allow the user to choose how many games they wish to play. They must play at least 3 games and no more than 5. The program must ask the user how many games they wish to play right from the start; if they say less than 3 or more than 5, you must prompt them again, this time with instructions on the limits
Can someone help me add this part of the code to my existing code? This is what I have so far: import java.util.*;
import java.util.Scanner;
public class Guess
{
public static final int max=100;
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int gameCount = 0;
int guessCount = 0;
int totalGuesses = 0;
int maxGuess = 0;
String playAgain="y";
giveIntro();
while(playAgain.charAt(0)=='y' || playAgain.charAt(0)=='Y')
{
guessCount=playGame();
totalGuesses=totalGuesses+guessCount;
maxGuess=Math.max(maxGuess,guessCount);
gameCount++;
System.out.print("Do you want to play again?");
playAgain=console.next();
while((playAgain.length()==0) || playAgain.charAt(0)!='y' &&playAgain.charAt(0)!='Y' && playAgain.charAt(0)!='n' && playAgain.charAt(0)!='N')
{
System.out.println("Please enter yes or no.");
System.out.print("Do you want to play again?");
playAgain=console.next();
}
}
System.out.println();
results(totalGuesses, gameCount, maxGuess);
}
public static void giveIntro()
{
System.out.println("Let's play a number guessing game.");
System.out.println("How many games do you want to play?");
}
public static int playGame()
{
Scanner console=new Scanner(System.in);
Random r= new Random();
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
{
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;
}
} Thank you in advance for your help!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
import javautil public class Guess public static final int max 100 public static void mainString args Scanner console new ScannerSystemin int gameCoun...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started