I\'m having a lot of trouble with figuring out how to find a minimum value from
ID: 3556616 • Letter: I
Question
I'm having a lot of trouble with figuring out how to find a minimum value from a set of values inputted with Scanner. I got a hint that I should be using a minimum loop but I really can't figure out how to make it work. I'm supposed to print the minimum amount of guesses it took the user to guess a number in any round, but as of now, the "best game" part keeps printing the number of guesses from the very last round. Sorry if this isn't making sense, but hopefully my code makes more sense! The problem is bold/italicized.
Also, I'm not allowed to use arrays or return multiple values from a method (that's why there's a do/while loop in the middle of the main method).
import java.util.*;
public class GuessingGame {
public static final int MAX_NUMBER = 101; // Set at one number above the range asked in the
// game so that all numbers in range are included.
public static void main(String[] args) {
introduction();
Scanner console = new Scanner(System.in);
Random rand = new Random();
int numOfGames = 0;
int totalGuesses = 0;
int numOfGuesses;
String answer;
// Runs one game and then prints prompt asking user if they want to play again. Replays game
// for as many times as user answers yes or with any word that begins with the letter "y".
do {
numOfGuesses = oneGame(console, rand);
totalGuesses += numOfGuesses;
numOfGames++;
System.out.print("Do you want to play again? ");
answer = console.next();
System.out.println();
} while (answer.startsWith("y") || answer.startsWith("Y"));
int bestGame = bestGame(numOfGuesses, numOfGames);
reportResults(totalGuesses, numOfGames, bestGame);
}
// Prints out an introduction to the game in the form of a haiku.
public static void introduction() {
System.out.println("A random number");
System.out.println("has been picked. Choose correctly,");
System.out.println("or I'll give a hint.");
System.out.println();
}
// Picks a random number between 1 and 100 and plays one round of the game. Prompts user
// for guesses and gives hints on whether the number is lower or higher. Prints a statement
// telling user how many guesses they made once they guess the correct number and also
// returns the number of guesses.
public static int oneGame(Scanner console, Random rand) {
System.out.println("I'm thinking of a number between 1 and " + (MAX_NUMBER - 1) + "...");
int number = rand.nextInt(MAX_NUMBER);
int guess = 0;
int numOfGuesses = 0;
while (guess != number) {
System.out.print("Your guess? ");
guess = console.nextInt();
numOfGuesses++;
if (guess > number) {
System.out.println("It's lower.");
} else if (guess < number) {
System.out.println("It's higher.");
} else {
if (numOfGuesses == 1) {
System.out.println("You got it right in " + numOfGuesses + " guess!");
} else {
System.out.println("You got it right in " + numOfGuesses + " guesses!");
}
}
}
return numOfGuesses;
}
public static int bestGame(int numOfGuesses, int numOfGames) {
int min = numOfGuesses;
for (int i = 1; i <= numOfGames - 1; i++) {
if (numOfGuesses < min) {
min = numOfGuesses;
}
}
return min;
}
// Prints statistics of games played using given # of total guesses and # of total games.
public static void reportResults(int totalGuesses, int numOfGames, int bestGame) {
System.out.println("Overall results:");
System.out.println("Total games = " + numOfGames);
System.out.println("Total guesses = " + totalGuesses);
System.out.printf("Guesses/game = %.1f ", (double)(totalGuesses) / numOfGames);
System.out.print("Best game = " + bestGame);
}
}
Explanation / Answer
import java.util.*;
public class GuessingGame {
public static final int MAX_NUMBER = 101; // Set at one number above the range asked in the
// game so that all numbers in range are included.
public static void main(String[] args) {
introduction();
Scanner console = new Scanner(System.in);
Random rand = new Random();
int numOfGames = 0;
int totalGuesses = 0;
int[] numOfGuesses = new int[1000];
String answer;
// Runs one game and then prints prompt asking user if they want to play again. Replays game
// for as many times as user answers yes or with any word that begins with the letter "y".
do {
numOfGuesses[numOfGames] = oneGame(console, rand);
totalGuesses += numOfGuesses;
numOfGames++;
System.out.print("Do you want to play again? ");
answer = console.next();
System.out.println();
} while (answer.startsWith("y") || answer.startsWith("Y"));
int bestGame = bestGame(numOfGuesses, numOfGames);
reportResults(totalGuesses, numOfGames, bestGame);
}
// Prints out an introduction to the game in the form of a haiku.
public static void introduction() {
System.out.println("A random number");
System.out.println("has been picked. Choose correctly,");
System.out.println("or I'll give a hint.");
System.out.println();
}
// Picks a random number between 1 and 100 and plays one round of the game. Prompts user
// for guesses and gives hints on whether the number is lower or higher. Prints a statement
// telling user how many guesses they made once they guess the correct number and also
// returns the number of guesses.
public static int oneGame(Scanner console, Random rand) {
System.out.println("I'm thinking of a number between 1 and " + (MAX_NUMBER - 1) + "...");
int number = rand.nextInt(MAX_NUMBER);
int guess = 0;
int numOfGuesses = 0;
while (guess != number) {
System.out.print("Your guess? ");
guess = console.nextInt();
numOfGuesses++;
if (guess > number) {
System.out.println("It's lower.");
} else if (guess < number) {
System.out.println("It's higher.");
} else {
if (numOfGuesses == 1) {
System.out.println("You got it right in " + numOfGuesses + " guess!");
} else {
System.out.println("You got it right in " + numOfGuesses + " guesses!");
}
}
}
return numOfGuesses;
}
public static int bestGame(int[] numOfGuesses, int numOfGames) {
int min = numOfGuesses[0];
for (int i = 1; i <= numOfGames - 1; i++) {
if (numOfGuesses[i] < min) {
min = numOfGuesses[i];
}
}
return min;
}
// Prints statistics of games played using given # of total guesses and # of total games.
public static void reportResults(int totalGuesses, int numOfGames, int bestGame) {
System.out.println("Overall results:");
System.out.println("Total games = " + numOfGames);
System.out.println("Total guesses = " + totalGuesses);
System.out.printf("Guesses/game = %.1f ", (double)(totalGuesses) / numOfGames);
System.out.print("Best game = " + bestGame);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.