I\'m writing a code for a random number guessing game. I basically have everythi
ID: 658279 • Letter: I
Question
I'm writing a code for a random number guessing game. I basically have everything figured out except I need the whole thing to run as a while loop so that at the end I can prompt the player to answer whether they want to play again ("Y/N") and then it'll restart the program from the beginning but I don't know the best way to do that now. My current code is below. as you can see I already have put it in a while loop but I don't know the best way to go about finishing it. I also need to create an overall results report that gives - number of games played, total guesses, average guesses per game, and the best game the player played (game with least amount of guess):
import java.util.*; //so I can use scanner
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random ();
int max = 1000;
int numberToGuess = rand.nextInt(max) + 1;
int numberOfTries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
boolean playAgain = true;
while (playAgain == true) {
System.out.println("I'm thinking of a number between 1 and " + max + "...");
while (win == false) {
System.out.print("Your guess? " + numberToGuess);
guess = input.nextInt();
numberOfTries++;
if (guess == numberToGuess) {
win = true;
} else if (guess > numberToGuess) {
System.out.println("It's lower.");
} else if (guess < numberToGuess) {
System.out.println("It's higher.");
}
}
if (numberOfTries == 1) {
System.out.println("You got it right in " + numberOfTries + " guess!");
} else {
System.out.println("You got it right in " + numberOfTries + " guesses!");
}
System.out.print("Do you want to play again? ");
}
}
}
}
Explanation / Answer
Here is the code for the question. Sample output also shown. Please rate the answer if it helped. Thank you very much.
import java.util.*; //so I can use scanner
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random ();
int max = 100;
int numberToGuess = rand.nextInt(max) + 1;
int totalTries=0,numberOfTries = 0, numOfGames=0, bestGuess=-1;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
boolean playAgain = true;
String ans;
while (playAgain == true) {
numberToGuess = rand.nextInt(max) + 1;
numberOfTries = 0;
win = false;
numOfGames++;
System.out.println("I'm thinking of a number between 1 and " + max + "...");
while (win == false) {
System.out.print(" Your guess? " );
guess = input.nextInt();
numberOfTries++;
if (guess == numberToGuess) {
win = true;
} else if (guess > numberToGuess) {
System.out.println("It's lower.");
} else if (guess < numberToGuess) {
System.out.println("It's higher.");
}
}
if (numberOfTries == 1) {
System.out.println("You got it right in " + numberOfTries + " guess!");
} else {
System.out.println("You got it right in " + numberOfTries + " guesses!");
}
totalTries += numberOfTries;
if(bestGuess == -1 || numberOfTries < bestGuess)
bestGuess = numberOfTries;
System.out.print("Do you want to play again? ");
ans = input.next();
if(ans.equalsIgnoreCase("y") || ans.equalsIgnoreCase("yes"))
playAgain = true;
else
playAgain =false;
}
System.out.println(" Overall results:");
System.out.println("Total games = "+numOfGames);
System.out.println("Total guesses = "+totalTries);
System.out.println("Guesses/game = "+ 1.0 * totalTries/numOfGames);
System.out.println("Best guess = "+bestGuess);
}
}
output
I'm thinking of a number between 1 and 100...
Your guess? 40
It's higher.
Your guess? 60
It's lower.
Your guess? 50
It's higher.
Your guess? 55
It's lower.
Your guess? 53
It's lower.
Your guess? 51
You got it right in 6 guesses!
Do you want to play again? y
I'm thinking of a number between 1 and 100...
Your guess? 80
It's lower.
Your guess? 60
It's lower.
Your guess? 40
It's lower.
Your guess? 20
It's lower.
Your guess? 10
It's lower.
Your guess? 5
It's higher.
Your guess? 8
It's lower.
Your guess? 7
You got it right in 8 guesses!
Do you want to play again? y
I'm thinking of a number between 1 and 100...
Your guess? 30
It's higher.
Your guess? 50
It's higher.
Your guess? 70
It's higher.
Your guess? 90
It's higher.
Your guess? 97
You got it right in 5 guesses!
Do you want to play again? n
Overall results:
Total games = 3
Total guesses = 19
Guesses/game = 6.333333333333333
Best guess = 5