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

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

(int) (Math.random() * 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]);
}

猜你喜欢

转载自www.cnblogs.com/leemlzm/p/12044541.html