RandomStudent.java


Below is the syntax highlighted version of RandomStudent.java from §1.4 Arrays.


/******************************************************************************
 *  Compilation:  javac RandomStudent.java
 *  Execution:    java RandomStudent
 *
 *  Print a random student.
 *
 ******************************************************************************/


public class RandomStudent {
    public static void main(String[] args) {
        String[] names = {
            "Clelia Zacharias", "Hannah Xu",
            "Virginia Wylly",   "Wendy Wu",
            "Ashely Wolf",      "Eric Whitman",
            "Will Weidman",     "Sharon Weeks",
            "Mary Wathall",     "Sarah Wang",
            "Michael Wang",     "Madeleine Walsh"
        };

        int n = names.length;
        int r = (int) (Math.random() * n);
        System.out.println(names[r]);
    }
}


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