JAVA基础学习-对数组随机赋值并排序

随机数的两种方法

1、Math.random() 会得到一个0-1之间的随机浮点数,然后乘以100,并强转为整型即可。(int) (Math.random() * 100);

2、new Random().nextInt(100);

public static void main(String[] args) {
int[] a = new int[5];
for (int i = 0; i < 5; i++) {
a[i] = (int) (Math.random()*100);
System.out.println("数组中的各个随机数是:"+a[i]);
}
Arrays.sort(a);
for (int i = 0; i < a.length; i++) {
System.out.println("数组中的排序后随机数是:"+a[i]);
}
System.out.println("数组中最小的数为"+a[0]);
}
posted @ 2019-12-15 16:25  李振明  阅读(904)  评论(0)    收藏  举报