Write the class RandomGuess.Java where a user inputs a jumber between 1 and 1,00
ID: 3858957 • Letter: W
Question
Write the class RandomGuess.Java where a user inputs a jumber between 1 and 1,000, then determine and display if the initial guess was correct, too, high, or too low, and add a loop that continuously prompts the user for the nmber, inndicating whether the guess is high or low, utnil the user enters the correct value. Once the player has figured out the correct number, ask if they would like to play again using the Scanner class. If yes, restart the game with a new random number. If not, use the System.out.println(); method to display a message that thanks the user for playing the game.
Explanation / Answer
import java.util.Scanner;
class RandomGuess{
public static void main (String[] args) {
String str = null;
do {
final int answer = 684; //some random number to find.
System.out.println("Guess a number");
Scanner input = new Scanner(System.in);
guess = input.nextInt();
do{
if (guess < 1 || guess > 1000){
System.out.println("That is not a valid entry. Please try again: ");
guess = input.nextInt();
}else if (guess > answer){
System.out.println("Too high, Try Again: ");
guess = input.nextInt();
}else if (guess < answer){
System.out.println("Too low, Try Again: ");
guess = input.nextInt();
}
} while (guess != answer);
System.out.println("Entered number is correct");
System.out.println("Do you want to play again? (yes/no) : ");
str = input.nextLine();
}while(str.equalsIgnoreCase("yes"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.