*7.7 (Count single digits) Write a program that generates 100 random integers between
0 and 9 and displays the count for each number. (Hint: Use an array of ten integers,
say counts, to store the counts for the number of 0s, 1s, . . . , 9s.)
*7.7(计算单个数字)编写一个程序,生成0到9之间的100个随机整数,并显示每个数字的计数。(提示:使用一个由十个整数组成的数组,比如counts,来存储0、1、…、9的计数。)
代码如下:
public class Unite7Test7
{
public static void main(String[] args)
{
int [] arr = new int [10];
for(int i =0;i<=99;i++)
{
int num = (int)(Math.random()*10);
arr[num]++;
}
for(int i = 0;i<=9;i++)
{
System.out.println(i+"的数量是"+arr[i]+"\t");
}
}
}
结果如下: