提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用数组的压模程序
/* from n o w j a v a . c o m*/ import java.security.SecureRandom; public class Main { public static void main(String[] args) { SecureRandom randomNumbers = new SecureRandom(); int[] frequency = new int[7]; // array of frequency counters // roll die 6,000,000 times; use die value as frequency index for (int roll = 1; roll <= 6000000; roll++) ++frequency[1 + randomNumbers.nextInt(6)]; System.out.printf("%s%10s%n", "Face", "Frequency"); /* from 时 代 Java 公 众 号 - nowjava.com */ // output each array element's value