This is an introductory class. please use SIMPLE METHODS thank you! In chapter 1
ID: 3738930 • Letter: T
Question
This is an introductory class. please use SIMPLE METHODS thank you! In chapter 1 you create a class called RandomGuess. in this game players guess a number, the application generates a random number, and the players determined whether they were correct, and you created a display message to indicate whether the players guess was correct, too high, or too low. Now, add a loop that continuously prompts the user for a number, indicating whether the guess is high or low, until the user enters the correct value. after the user correctly guesses the number, display a count of the number of attempts it took. save the file as RandomeGuess3Explanation / Answer
RandomGuess.java
import java.util.Random;
import java.util.Scanner;
public class RandomGuess {
// Scanner object is used to get the inputs entered by the user
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
//Declaring variables
int secret, guess,count=0;
//This loop will continue to execute until the user enters either 'Y' or 'y'
while (true) {
secret=getSecretNumber();
//this loop continues to execute until the user correctly guesses the number;
while (true) {
guess=getUserEnteredNumber();
if (guess == secret) {
System.out.println("** Correct **");
System.out.println("No of attempts it took :"+count);
break;
} else {
if (guess > secret) {
System.out.println("** Too High **");
count++;
continue;
} else {
System.out.println("** Too Low **");
count++;
continue;
}
}
}
// Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to Play Again(Y/N) ::");
char ch = sc.next(".").charAt(0);
if (ch == 'Y' || ch == 'y')
continue;
else {
System.out.println(":: Thanks for playing the game. ::");
break;
}
}
}
private static int getUserEnteredNumber() {
int guess;
while(true)
{
//Getting the user guess entered by the user
System.out.print("Guess the number (Between 1-1000):");
guess = sc.nextInt();
if(guess<1 || guess>1000)
{
System.out.println("Invalid Guess.Must be between 1-1000");
continue;
}
else
break;
}
return guess;
}
private static int getSecretNumber() {
int secret;
//Creating the Random class object
Random generator = new Random();
//generating the random number
secret = generator.nextInt((1000 - 1) + 1) + 1;
return secret;
}
}
_____________________
Output:
Guess the number (Between 1-1000):500
** Too High **
Guess the number (Between 1-1000):250
** Too Low **
Guess the number (Between 1-1000):350
** Too High **
Guess the number (Between 1-1000):300
** Too High **
Guess the number (Between 1-1000):275
** Too High **
Guess the number (Between 1-1000):260
** Too Low **
Guess the number (Between 1-1000):265
** Too Low **
Guess the number (Between 1-1000):267
** Too Low **
Guess the number (Between 1-1000):269
** Too Low **
Guess the number (Between 1-1000):270
** Too Low **
Guess the number (Between 1-1000):273
** Correct **
No of attempts it took :10
Do you want to Play Again(Y/N) ::n
:: Thanks for playing the game. ::
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.