首先,创建一个双精度数组-
double[] val = new double[10];
现在,在循环中生成并显示随机数,循环直到上述数组的长度。我们在这里将nextInt用于随机数-
for (int i = 0; i < val.length; i++) { val[i] = new Random().nextInt(100); System.out.println(val[i]); }
现在,获取最小值和最大值。将随机数组的每个值与MIN和MAX值进行比较-
double min = Double.MAX_VALUE; double max = Double.MIN_VALUE; for (int i = 0; i < val.length; i++) { if (val[i] < min) min = val[i]; if (val[i] > max) max = val[i]; }
import java.util.Random; public class Demo { public static void main(String[] args) { double[] val = new double[10]; double min = Double.MAX_VALUE; double max = Double.MIN_VALUE; System.out.println("Random number array..."); for (int i = 0; i < val.length; i++) { val[i] = new Random().nextInt(100); System.out.println(val[i]); } for (int i = 0; i < val.length; i++) { if (val[i] < min) min = val[i]; if (val[i] > max) max = val[i]; } System.out.println("Minimum element = " + min); System.out.println("Maximum element = " + max); } }
输出结果
Random number array... 75.0 42.0 47.0 71.0 12.0 70.0 86.0 97.0 14.0 41.0 Minimum element = 12.0 Maximum element = 97.0