Hi, I need help developing a program in java thats a guessing game Modify it so
ID: 3666754 • Letter: H
Question
Hi, I need help developing a program in java thats a guessing game
Modify it so that the program plays a number guessing game with the user.
i. First, the program picks a random integer between 1 and 100 (inclusive), then it accepts guesses from the user until the user guesses the number correctly. After each incorrect guess, the program will tell the user whether the correct answer is higher or lower than the guess. Once the user types the correct number, the game ends and the program reports how many guesses were made. An example game is shown below:
I'm thinking of a number between 1 and 100...
Your guess? 50
It's lower.
Your guess? 25
It's higher.
Your guess? 35
It's lower.
Your guess? 30
It's higher.
Your guess? 32
It's lower.
Your guess? 31
You guessed it in 6 guesses!
ii. After each game ends and the number of guesses is shown, the program asks the user if he/she would like to play again. Assume that the user will type a oneword string as the response to this question. A new game should begin if the user's response starts with a lower- or upper-case Y. For example, answers such as "y", "Y", "yes", "YES", "Yes", or "yeehaw" all indicate that the user wants to play again. Any other response means that the user does not want to play again. For example, responses of "no", "No", "okay", "0", "certainly", and "hello" are all assumed to mean no.
iii. Once the user chooses not to play again, the program prints overall statistics about all games played: the total number of games, total guesses made in all games, average number of guesses per game (as a real number rounded to the nearest tenth), and best game (fewest guesses needed to win any one game) are displayed.
I'm thinking of a number between 1 and 100...
Your guess? 50
It's higher.
Your guess? 75
It's lower.
Your guess? 65
It's lower.
Your guess? 64
You guessed it in 4 guesses!
Play again? YES
I'm thinking of a number between 1 and 100...
Your guess? 37
You guessed it in 1 guesses!
Play again? y
I'm thinking of a number between 1 and 100...
Your guess? 50
It's lower. Your guess? 25
It's lower. Your guess? 13
You guessed it in 3 guesses!
Play again? No
Your overall results: Total games = 3 Total guesses = 8 Guesses/game = 2.7 Best game = 1
Thank you very much for your help
Explanation / Answer
import java.util.*;
public class GuessingGame {
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
boolean doneWithGame = false;
int totalGames = 0;
int totalGuesses = 0;
int bestGame = 0;
do
{
System.out.println("I'm thinking of a number between 1 and 100...");
int numOfGuesses = guessingGame(console);
totalGames++;
totalGuesses += numOfGuesses;
bestGame = bestGameComparison( bestGame, numOfGuesses);
System.out.print("Play again?");
String playAgain = console.next();
if (playAgain.charAt(0) == 'y' || playAgain.charAt(0) == 'Y')
{
System.out.println();
}
else
{
doneWithGame = true;
}
}while( doneWithGame != true);
resultPrintOut(totalGames,totalGuesses, bestGame);
}
/**
* Creates a random number and prompts the user to make a guess. Once number is guessed, it tells how many guesses
* it took and returns back to the main method.
* @param console : allows user input to guess number.
* @return guessCounter: number of guesses it took to figure out random number.
*/
public static int guessingGame(Scanner console)
{
Random rand = new Random();
int randomNum = rand.nextInt(100) + 1;
int guess;
int guessCounter = 0;
do
{
System.out.print("Your guess? ");
guess = console.nextInt();
guessHints(guess, randomNum);
guessCounter++;
}while (guess !=randomNum);
System.out.println("You guessed it in " + guessCounter + " guesses!");
return guessCounter;
}
/**
* Prints out a message telling the user if their input guess is higher or lower than the number they are trying to guess.
* @param guess : the guess that the user input.
* @param answer : the random number that the code generated, and the user is trying to guess
*/
public static void guessHints(int guess, int answer)
{
if (guess < answer)
{
System.out.println("It's higher.");
}
else if( guess > answer)
{
System.out.println("It's lower.");
}
else
{
//null
}
}
/**
* Determines the best game with the lowest number of guesses. The lowest number of guesses it took is the best game, and returned
* to the best game integer.
* @param currentBestGame : The current best game. If it is 0, it is automatically replaced with newNumOfGuesses
* @param newNumOfGuesses : current games number of guesses it took to find number.
* @return the lowest number of guesses between the two integers.
*/
public static int bestGameComparison(int currentBestGame, int newNumOfGuesses)
{
int bestGame = 0;
if(currentBestGame == 0)
{
bestGame = newNumOfGuesses;
}
else if(newNumOfGuesses < currentBestGame)
{
bestGame = newNumOfGuesses;
}
else
{
bestGame = currentBestGame;
}
return bestGame;
}
/**
* Prints out the results of the guessing games after the user is done.
* @param totalGames : Total number of games played.
* @param totalGuesses : Total number of guesses over all games played
* @param bestGame : lowest number of guesses for a game.
*/
public static void resultPrintOut( int totalGames, int totalGuesses, int bestGame)
{
System.out.println();
System.out.println("Your overall results:");
System.out.println("Total games = " + totalGames);
System.out.println("Total guesses = " + totalGuesses);
System.out.println("Guesses/game = " + (double)totalGuesses / (double)totalGames);
System.out.println("Best game = " + bestGame);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.