Sunday, 16 December 2012

A Java Program That Rolls A Pair OF Dice and Sums Up the Outcomes. Now Who Wants To Gamble ;)


import java.util.Scanner;
public class DiceRoll
{
     private static final int dice = 6;

     public static void main( String[] args )
     {
          Scanner input = new Scanner( System.in );
           int x = 0;
           int y = 0;
           int z = 0;
           /*Rolls and sums up outcome of Dice till the loop breaks if 7 is entered at  
           prompt*/
          while ( z != 7 ) {
             System.out.println( "Press Any Number To Roll First Die" );
              x = input.nextInt();
               x = (int) (Math.random() * dice) + 1;
                System.out.println( "First Roll = " + x );
                 System.out.println( "Press Any Number To Roll Second Die" );
                  y = input.nextInt();
                   y = (int) (Math.random() * dice) + 1;
                    System.out.println( "Second Roll = " + y );
                     System.out.println( "You Rolled a " + (x + y) + "\n" );
                      System.out.println( "Press 7 To quit Or Any Number To Keep On Rolling" );
                       z = input.nextInt();
          }
     }
}

A Little Java Game That Asks A User To Guess A Random Number From The Width Specified By The User.


import java.util.Scanner;
public class GuessNumber
{
    public static void main( String[] args )
    {
        Scanner input = new Scanner( System.in );

        System.out.print( "Please Enter A Number To Be Shuffled: " );
        int x = input.nextInt();//Asks for width Of Random Number to Be Generated
       
        int y = (int) (Math.random() * x);//Generates Random Number
        System.out.print( "Now Guess The Random Number Generated: " );

        int z = input.nextInt();//Asks For The User's Number

        /*Requests for User number and checks if it
        is the newly generated random number*/
        while (z != y) {
            System.out.println( "\nThis is the right Number " + y );
             System.out.print( "Please Try again Till You Get the Right Number: " );
               z = input.nextInt();
                y = ( int ) ( Math.random() * x );
        }
        System.out.println( "CORRECT" );//Breaks Loop and Prints if Number Matches
    }
}