Post 6 - Number Guessing Game using Java

Today we will learn to build Number Guessing Game using Java .

✅ Problem Statement :

Build a Number Guessing Game in which Player ( Computer ) has to keep one number from 1 - 10 in his mind and Player 2 ( User ) will try to guess that number . 
Twist is Player 2 will get only 3 attempts to guess the correct number. If he failed to guess correct number other player will win the game.
Player 2 can play game as many times as he want. 

Interesting ❓  Let's get Started 😉

✅ Concepts Covered in This Post :

  • Importing java.lang.Math package
  • Use of Math.random ( )
  • Do.. While ( ) Looping Statement  

📑 Let's Code :

Step 1 : Follow the below steps -
Open Eclipse ➝ Create New Project (NumberGuessing) ➝ Create Package (numberGuessing) ➝ Create 2 Java classes (NumberGuessing.java and Main.java)

NumberGuessing.java will contain methods to generate random number and a method to compare 2 two numbers
• Main.java will contain main ( ) method to test this functionalities.

Step 2 : Now, we will complete NumberGuessing.java class -
This class contains -
  • Importing java.lang.Math package
  • Method for generation random number 
  • Method for comparing two numbers
Here is the sample code  for NumberGuessing.java class -

NumberGuessing.java 1.1

🔴 Concept To Learn :

➊ Math.random () Function :

→ This method returns random number of Double type. 
→ To use this method you need to import java.lang.Math package.
→ This function always generates a value between 0 and 1 . So if you want to generate values between specific range , you have to multiply the returned value with the magnitude of the range.
For Ex- if you want to generate values between 0 and 20 , then multiple returned value by 20.  

 In the above code , we need to generate number between 1 to 10 . So we multiple returned value by 10.
 As Math.random() returns value of Double type,  we converted that double value into int ( Line No 12 from  Code). 

Step 3 : In this step, we will complete Main.java .
This class contains - 
        • main ( ) method.
        • do ... while ( ) loop.
        • Object of Scanner class and NumberGuessing.java class.
        • if .. else statement.
        • Taking input , processing and printing output. 

Functionalities to Implement in this class :
  1. Create  Objects of Scanner class and NumberGuessing.java Class
  2. Generate Random Number using generateRandomMethod() of NumberGuessing.java class
  3. Give 3 chances to user to guess the number . So use do ..  while ( ) loop
  4. Take input from user to enter his guessed number. 
  5. Check whether Random Number and Entered Number are Same or not 
  6. If both numbers are NOT SAME , give another chance to user. If both numbers are SAME declare user as a Winner and End the Game. 
  7. Ask user if he wants to play again, If yes then give 3 more chances and generate new random number. If no then end the game.
Here is the sample code for Main.java -

Main.java 2.1

Main.java 2.2

🔴 Concept To Learn :

① Do ..  While Loop -
-  This statement executes a block of code until the given condition is True .
-   Do .. While loop is similar to While Loop . But the difference is that, Do .. While loop executes block of code firsts and then check the condition. In While Loop we first check the condition and then executes a block of code if the condition is True.
-   So,  In  Do... While Loop Block of Code gets executed at least once.
-   Do .. While Loop is also Called as Exit Control Loop.

◼ Syntax -
Syntax of Do... While() Loop
 

Step 4 : Run the Code and Check the Output 😊

Here is the Sample Output -

Output 


And here will complete one more simple project using Java !!
Hope you understand the concepts very well .
Check Source Code of the project here 👉 Source Code 

All the Best 👍👍👍 
Happy Learning 😇😇😇❗❗


Post a Comment

Comments :

Previous Post Next Post