编写一个程序,生成0和9之间的100个随机整数,然后显示每一个数出现的次数。
提示:使用(int) (Math.random() * 10)产生 0 和 9 之间的100个随机整数,使用一个名为counts的由10个整数构成的数组存放 0 ~ 9 的个数。
package pack2; public class Statistics { public static void main(String[] args) { statistics(); } /**统计一位数的个数*/ public static void statistics() { int[] counts = new int[10]; for (int i = 0; i < 100; i++) { int number = (int)(Math.random() * 10); counts[number]++; } for (int i = 0; i < counts.length; i++) System.out.println(i+" occurs "+counts[i]+(counts[i] > 1 ? " times" : " time")); } }