Create a game in Java that players guess a number between 1 and 1000. Have the a
ID: 3856186 • Letter: C
Question
Create a game in Java that players guess a number between 1 and 1000. Have the application display a message indicating whether the player's guess was correct, too high, or too low. Add a loop as well that continuously prompts the user for the number, indicating correct, too high, or too 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. 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
Here is your program below. Let me know if you face any issue in comments.
import java.util.Random;
import java.util.Scanner;
public class RandomGame {
public static void main(String[] args) {
int randomnumber;
Scanner sc = new Scanner(System.in);
Random rand = new Random();
boolean done = false;
String op;
while (!done) {
int guessed = 0; // guessed flag
int count = 0;
int guess;
randomnumber = rand.nextInt(1000) + 1; // generating random number
System.out.println("Guess My Number Game ");
while (guessed == 0) { // stop if guessed right
count++;
System.out.print(" Enter a guess : ");
guess = Integer.parseInt(sc.next());
if (guess > randomnumber) { // if guessed number is greater than
// generated number
System.out.println("Too High , Try Again.");
} else if (guess < randomnumber) {// if guessed number is less
// than generated number
System.out.println("Too Low , Try Again.");
} else {// if guessed number is equal to generated number
System.out.println("Good Guess. Its Correct.");
guessed = 1;
}
}
System.out.println("Congratulation. You guessed it right in " + count + " guesses.");
System.out.print(" Do you want to play again ? (y/n) ");
op = sc.next();
if(op.equalsIgnoreCase("y")) {
done = false;
} else {
done = true;
}
}
sc.close();
System.out.println(" Thanks for playing the game.");
}
}
Sample Run: -
Guess My Number Game
Enter a guess : 500
Too High , Try Again.
Enter a guess : 250
Too High , Try Again.
Enter a guess : 125
Too Low , Try Again.
Enter a guess : 150
Too Low , Try Again.
Enter a guess : 175
Too Low , Try Again.
Enter a guess : 200
Too Low , Try Again.
Enter a guess : 225
Too High , Try Again.
Enter a guess : 210
Too Low , Try Again.
Enter a guess : 215
Too High , Try Again.
Enter a guess : 212
Good Guess. Its Correct.
Congratulation. You guessed it right in 10 guesses.
Do you want to play again ? (y/n) y
Guess My Number Game
Enter a guess : 123
Too Low , Try Again.
Enter a guess : 300
Too Low , Try Again.
Enter a guess : 500
Too High , Try Again.
Enter a guess : 400
Too High , Try Again.
Enter a guess : 250
Too Low , Try Again.
Enter a guess : 350
Too High , Try Again.
Enter a guess : 325
Too Low , Try Again.
Enter a guess : 330
Too High , Try Again.
Enter a guess : 328
Too Low , Try Again.
Enter a guess : 329
Good Guess. Its Correct.
Congratulation. You guessed it right in 10 guesses.
Do you want to play again ? (y/n) n
Thanks for playing the game.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.