Friday, March 24, 2023

JAVA Guessing numbers game Program.#Bluej#blogger.com

If want to play some funny games in java .Then try this.

 import java.io.*;

class GuessingGame

{

    public static void main(String []args) throws IOException   

    {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n=1+(int)(50*Math.random());

        int K=5;

        int i,g;

        System.out.println("A number is chosen"+ " between 1 to 50."+ "Guess the number"+ " within 5 trials.");

        for (i=0;i<K;i++) 

        {

            System.out.println("Guess the number:");

            g=Integer.parseInt(br.readLine());

            if (n==g) 

            {

                System.out.println("Congratulations! You guessed the number.Hrrrrrrrrrrrruuuuuhhha");

                break;

            }

            else if (n>g && i!=K-1) 

            {

                System.out.println("The number is greater than "+g);

            }

            else if (n<g && i!=K-1)

            {

                System.out.println("The number is less than "+g);

            }

        }

        if (i==K) 

        {

            System.out.println("You have exhausted"+" the trials!!!!");

            System.out.println("The number was =" + n);

        }

    }

}

OUTPUT:


Then enjoy the game.


If like the game for timepassing then leave a comment below.

1 comment:

DHIRO'S BLOG said...

Very good! Keep it up!

Its entertaining.