题目:随机生成 50个数字(整数),每个数字的范围是[10,50],统计每个数字出现的次数以及出现次数最多的数字与它的个数,最后将每个数字及其出现次数打印出来,如果某个数字出现次数为0,则不要打印它。打印时按照数字的升序排列。
思路:数字的范围是10到50之间,就是最多有41个数字,定义一个count[]数组,int [] count= new int [41];,随机出现一个数字就 count[随机出现的数字-10]++ 就可以计算出每个数字出现的次数。
public class CountRandom
{
public static void main(String[] args)
{
Random r =
new Random();
int [] count=
new int [41
];
for(
int i=1;i<=50;i++
)
{
int n=r.nextInt(41)+10
;
count[n-10]++
;
System.out.print(n+" "
);
if(i==0
) {System.out.println();}
}
for(
int j=0;j<41;j++
) {
System.out.print((j+10)+":"+count[j]+" "
);
if((j+1)==0
) {System.out.println();}
}
}
}
转载于:https://www.cnblogs.com/lhh666/p/11493789.html
相关资源:java随机生成50个数字并统计出现次数