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

Write a guessing game in which the computer picks a random integer between 1 and

ID: 3538550 • Letter: W

Question

Write a guessing game in which the computer picks a random integer between 1 and 100 ( inclusive), and you have to guess what it is. Your game should follow these guidelines:


a. Include error checking to ensure that the player can't enter something below 1 or above 100.

b. The player has a maximum of 5 tries to correctly guess the number. If s/he can't get it after 5 tries, s/he loses the game. Note that invalid input ( something below 1 or above 100 ) should NOT count against this number of tries.

c. After each valid guess, display a message saying whether the guess was too high, too low, or correct. Also display the number of guesses remaining.

d. If the player is able to correctly guess the number, display a message saying how many tries it took him/her to do so.

e.If the player is unable to correctly guess the number after 5 valid tries, display a message saying what the correct number was.

f. Allow the player to repeat the game as many times as s/he wishes.


Follow standard Java programming conventions for variableNames, ClassNames,CONSTANT_NAMES

Explanation / Answer

import java.util.Scanner;

import java.io.IOException;

import java.util.Random;


public class RandomGame

{

public static void main(String[] args) throws IOException

{

Random rand = new Random();

int numberToGuess = 0;

int numberOfTries = 0;

int guess;

int MAX_TRIES = 5;

boolean win = false;

while(numberToGuess == 0) {

numberToGuess = rand.nextInt(101);

}

Scanner input = new Scanner(System.in);

while(win == false && numberOfTries < MAX_TRIES) {

System.out.println("Guess a number between 1 and 100:");

guess = input.nextInt();

if(guess >= 1 && guess <= 100) {

numberOfTries++;

if (guess == numberToGuess) {

win = true;

}

else if (guess < numberToGuess) {

System.out.println("Your guess is too low");

}

else if (guess > numberToGuess) {

System.out.println("Your guess is too high");

}

}

}

if(win == true) {

System.out.println("you win and it took you " + numberOfTries +" tries");

}

else if(numberOfTries == MAX_TRIES) {

System.out.println("The number was " + numberToGuess);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote