Java || Random Number Guessing Game Using Random & Do/While Loop

Print Friendly, PDF & Email

This is a simple guessing game, which demonstrates the use of the “Random” class to generate random numbers. This program first prompts the user to enter a number between 1 and 1000. Using if/else statements, the program will check to see if the user obtained number is higher/lower than the pre defined random number which is generated by the program. If the user makes a wrong guess, the program will re prompt the user to enter in a new number, where they will have a chance to enter in a new guess. Once the user finally guesses the correct answer, using a do/while loop, the program will ask if they want to play again. If the user selects yes, the game will start over, and a new random number will be generated. If the user selects no, the game will end.


The "Random" Class
Do/While Loop
How To get Character Input
Custom Setw/Setfill In Java

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:

Welcome to My Programming Notes' Java Program.

I'm thinking of a number between 1 and 1000. Go ahead and make your first guess.

>> 900

Too high! Think lower.

>> 300

Too high! Think lower.

>> 100

Too low! Think higher.

>> 200

Too low! Think higher.

>> 350

You got it, and it only took you 5 trys!
Would you like to play again (y/n)? y


Make a guess (between 1-1000):

>> 300

Too low! Think higher.

>> 600

Too high! Think lower.

>> 500

Too high! Think lower.

>> 400

You got it, and it only took you 4 trys!
Would you like to play again (y/n)? n

Thanks for playing!!

Was this article helpful?
👍 YesNo

Leave a Reply