Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);

  

}

}

Explanation / Answer

//I've added comments for all the methods import java.util.Random; import java.util.Scanner; class GuessTheNumber { //Scanner instance for reading user input private static Scanner input = new Scanner(System.in); private int numberOfGuesses; //Variables to keep track total guesses and games public static int totalGuesses = 0 ,countGames = 0 , GuessMin = Integer.MAX_VALUE; public void play() { //Random instance for random generation Random randomNumber = new Random(); //Generate a random number in the range [1 00] int theNumber = randomNumber.nextInt(100) + 1; //Initialize no. of guesses to 0 numberOfGuesses = 0; //Prompt and read user input int guess = askForNumber(); //Keep prompting until user guesses correctly 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
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote