home / study / engineering / computer science / computer science questions and a
ID: 3719281 • Letter: H
Question
home / study / engineering / computer science / computer science questions and answers / this assignment will give you practice with while loops and pseudorandom numbers. you are going ...
Question: This assignment will give you practice with while loops and pseudorandom numbers. You are going t...
What are the correct comments are each line of code written including the braces?
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
private static Scanner input = new Scanner(System.in);
private int numberOfGuesses;
public static int totalGuesses = 0 ,countGames = 0 , GuessMin = Integer.MAX_VALUE;
public void play() {
Random randomNumber = new Random();
int theNumber = randomNumber.nextInt(100) + 1;
numberOfGuesses = 0;
int guess = askForNumber();
while(guess != theNumber) {
// deterime if guess is too low or too high
// tell the user
if(guess > theNumber)
System.out.println("Too high! try again.");
else if(guess < theNumber)
System.out.println("Too low! try again.");
else
break;
guess = askForNumber();
}
System.out.println("It took you " + numberOfGuesses + " turns to guess my number, which was "+theNumber);
if(numberOfGuesses < GuessMin){
GuessMin = numberOfGuesses;
}
totalGuesses = totalGuesses + numberOfGuesses;
}
private int askForNumber() {
int guess = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your guess between 1 and 100: ");
guess = sc.nextInt();
numberOfGuesses++;
return guess;
}
public static void main(String args[]) {
System.out.println("This program allows you to play a guessing game.+ "
+ " I will think of a number between 1 and 100 and will allow you to guess until + "
+ "you get it. For each guess, I will tell you "
+ "whether the right answer is higher or lower "
+ "than your guess.");
int playAgain;
GuessTheNumber game = new GuessTheNumber();
do {
game.play();
++countGames;
System.out.print("Play again? 1-yes, 0-no");
playAgain = input.nextInt();
} while(playAgain == 1) ;
System.out.println("Overall results:");
System.out.println("total games = "+countGames);
System.out.println("total guesses = "+ totalGuesses);
System.out.println("guesses/game = "+ (totalGuesses*1.0/countGames));
System.out.println("best game = "+ GuessMin);
}
}