My program plays a game where you guess a number between 1 and 5. It will tell y
ID: 3535660 • Letter: M
Question
My program plays a game where you guess a number between 1 and 5. It will tell you to guess higher or lower until you get the correct answer. After you complete the game, it will ask if you'd like to play again.
If you respond with any word starting with a y such as yes, yup, or yeehaw; it will start the game over. To end the game you simply type a word that does not start with y such as no, nope, hello, or dogs.
After you end the game it will show your "overall results" which include: Total games, Total guesses, Guesses/game, and Best game.
My only concern is involving the "Best game" which should list the lowest guesses you used out of all the games you played. EX: game 1: guessed 3 times, game 2: guessed 4 times, Best Game: 3 times
I need help here because i don't know how to make it do the lowest amount of guesses.
Much appreciated! Thank you.
(Please do not post a code of your own when responding or you will not receive a low rating. Instead edit my code so that it compiles correctly. Thank you).
import java.util.Scanner;
import java.util.Random;
public class GuessingGameBest
{
public static final int MAX = 5;
public static void main(String[] args)
{
//Create a scanner object to read from the keyboard
Scanner keyboard = new Scanner (System.in);
//Create a random object
Random number = new Random();
//Identifier declarations
int num;
int guesses;
int guess=MAX;
int totalGames=0;
int totalGuesses=0;
//Begins program
String playAgain;
haikuIntroduction();
do {
num = number.nextInt(MAX) + 1;
programIntroduction();
for (guesses = 1 ; guesses <= MAX ; guesses++) {
System.out.print("Your guess? ");
guess = keyboard.nextInt();
totalGuesses++;
if (guess > num) {
System.out.println("It's lower.");
}
else if (guess < num) {
System.out.println("It's higher.");
}
else if (guess == num) {
System.out.println("You got it right in " + guesses + " guesses!");
break;
}
}
totalGames++;
System.out.print("Do you want to play again? ");
playAgain = keyboard.next();
}
while (playAgain.startsWith("y") || playAgain.startsWith("Y"));
System.out.println();
System.out.println("Overall Results:");
System.out.println("Total games = " + totalGames);
System.out.println("Total guesses = " + totalGuesses);
double grade = (double)totalGuesses / (double)totalGames;
System.out.println("Guesses/game = " + grade);
System.out.println("Best game = " + guesses);
}
//Haiku introduction
public static void haikuIntroduction() {
System.out.println("Let's Play a Game Now.");
System.out.println("It is a Fun Guessing Game.");
System.out.println("Try Your Best, Begin.");
}
//Program introduction
public static void programIntroduction() {
System.out.println();
System.out.println("I'm thinking of a number between 1 and 5...");
}
}
Explanation / Answer
the changes are in line 20,45,46,58
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.