Source of RollDice.java


  1: import java.util.Scanner;
  2: import java.util.Random;

  4: /**
  5:  * A program that simulates rolling two d6.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class RollDice {

 11:     public static void main(String[] args) {
 12:         // create variables
 13:         Scanner kbd = new Scanner(System.in);
 14:         Random generator = new Random();
 15:         int d1, d2, dice;

 17:         // Introduce yourself
 18:         System.out.println("\n\n"
 19:                 + "I'm just rollin' some dice!\n\n");
 20:         System.out.println();
 21:         System.out.print("Press Enter...");
 22:         kbd.nextLine();
 23:         System.out.println();

 25:         // roll dice -- Math.random gives us a random number in range [0,1)
 26:         d1 = 1 + generator.nextInt(6);
 27:         d2 = 1 + generator.nextInt(6);
 28:         dice = d1 + d2;

 30:         // report the result
 31:         System.out.println("I got a " + d1 + " and a " + d2 + ".\n"
 32:                 + "The total is " + dice + ".");
 33:         System.out.println();
 34:         System.out.print("Press Enter...");
 35:         kbd.nextLine();
 36:         System.out.println();
 37:     }

 39: }