Skip to main content

simple Guessing Game in JAVA


package javaguessinggame1;
import java.util.*;
/**
 *@title Guessing  Program
 * @author Herdy
 */
public class JavaGuessingGame1 
{
//random number declaration 
    static Random r= new Random();
    static Scanner i =new Scanner(System.in);
    public static void main(String[] args)
    {
  //setting the variable round to 0

        int round=0;
        do {

 //add one to variable round while it is less than 3
            round++;
            System.out.println("Round: "+round);

//setting the variable com and guess to choose a random number from 0-1 only 
               double com=Math.floor(Math.random()*2),
               guess=Math.floor(Math.random()*2);
               System.out.println("Input 1 0 only");
               double player=i.nextDouble();

 //compare the value of player and com variable to guess(Random Number) variable  if both of them have //the same value to guess 
        if(player==guess && com==guess)
        {
            System.out.println("Ramdom Number "+ (int) guess);
            System.out.println("Player guess "+ (int) player);
            System.out.println("Computer "+ (int) com);
            System.out.println("Its a tie!!!");
            
        }
//compare the value of player and com variable to guess(Random Number) variable  if both of them have //different value to guess
        else if(player!=guess && com!=guess)
        {
            System.out.println("Ramdom Number "+ (int) guess);
            System.out.println("Player guess "+ (int) player);
            System.out.println("Computer "+ (int) com);
            System.out.println("Both of you got the wrong guess!!! \n \n");
        }
//checks if the player has the same value to guess and com has different value to guess
       else if(player==guess && com!=guess)
        {
            System.out.println("Ramdom Number "+ (int) guess);
            System.out.println("Player guess "+ (int) player);
            System.out.println("Computer "+ (int) com);
            System.out.println("Player got the correct guess!!! \n \n");
            
        }

//checks if the player has the same value to guess and com has different value to guess
       else if(player!=guess && com==guess)
        {
            System.out.println("Ramdom Number "+ (int) guess);
            System.out.println("Player guess "+ (int) player);
            System.out.println("Computer "+ (int) com);
            System.out.println("Computer got the correct guess\n \n");
            
        }
        System.out.println("End of Round "+round+"\n \n");

//checks if the round is eqauls to 3
        if(round==3)
        {
       System.out.println("End of the Game!! Thanks for Playing!!!");
        }
        
//limits the number loop
        }while(round<3);
        
        }
}

Comments