java 37.2 10_JavaSE 第三十七讲 作业详解 10.27

1、产生随机数

import java.util.Random;

public class RandomTest

{

public static void main(String[] args)

{

Random random = new Random();

for(int i = 0; i < 50; i++)

{

System.out.println(random.nextInt(41) + 10);

}

}

}

方法2:

import java.util.Random;

public class RandomTest

{

public static void main(String[] args)

{

Random random = new Random();

for(int i = 0; i < 50; i++)

{

double result = Math.random();

result *= 41;

int result2 = (int)result;

result2 += 10;

System.out.println(result2);

}

}

}

2、随机生成50个数字(整数),每个数字的范围是[10, 50]。统计每个数字出现的次数及出现次数最多的数字与它的个数,最后将每个数字及其出现次数打印出来。如果某个数字出现次数为0,不打印。打印时,按照数字升序排序。

import java.util.Random;

public class RandomTest2

{

public static void main(String[] args)

{

int[] count = new int[41];

Random random = new Random();

for(int i = 0; i < 50; i++)

{

int number = random.nextInt(41) + 10;

count[number - 10]++;

}

for(int i = 0; i < count.length; i++)

{

if(0 == count[i])

{

continue;

}

System.out.println((10 + i) + "出现次数:" + count[i]);

}

int max = count[0];

for(int i = 0; i < count.length; i++)

{

if(max < count[i])

{

max = count[i];

}

}

System.out.println("出现的最大次数为:" + max + "次");

for(int i = 0; i < count.length; i++)

{

if(max == count[i])

{

System.out.println(i + 10);

}

}

}

}

你可能感兴趣的:(java,37.2,10)