RollDie.java


Below is the syntax highlighted version of RollDie.java from §1.3 Conditionals and Loops.


/******************************************************************************
 *  Compilation:  javac RollDie.java
 *  Execution:    java RollDie
 *
 *  Simulate the roll of a fair six-sided die
 *  and print the resulting number.
 *
 *  % java RollDie
 *  4
 *
 *  % java RollDie
 *  1
 *
 ******************************************************************************/

public class RollDie {
    public static void main(String[] args) {
        int SIDES = 6;   // how many sides on the die?

        // roll should be 1 through SIDES
        int roll = (int) (Math.random() * SIDES) + 1;

        // print result
        System.out.println(roll);

    }
}



Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.