生成随机数,统计每个数的次数
编写程序,生成0和9之间的50个随机整数,每行打印10个生成的随机数,然后显示每一个数出现的次数。(提示:使用(int)(Math.random()10)产生0到9之间的随机整数。使用一个名为counts的由10个整数构成的数组存放0,1,…,9的个数。)
package com;
public class Demo1_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 0;
int[] counts = new int[10];
System.out.println("生成50个随机数:");
for (int count = 0; count < 50 ; count++){
int element = (int) (Math.random() * 10);
System.out.print(element+"\t");
n++;
if(n %10== 0) {
System.out.println();
}
counts[element]++;
}
for (int i = 0 ; i < 10 ; i++){
System.out.println(i + " 的统计次数是"+ counts[i] );
}
}
}