1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public class Test { public static void main(String[] args) { double[] array = new double[5]; for (int i=0; i<array.length; i++) { array[i] = 100*Math.random(); } System.out.println("源数组:"); for(int i=0; i<array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); System.out.println("最大值:" + MaxMin.getResult(array).getMax()); System.out.println("最小值:" + MaxMin.getResult(array).getMin()); } }
class MaxMin { public static class Result { private double max; private double min; public Result(double max, double min) { this.max = max; this.min = min; } public double getMax() { return max; } public double getMin() { return min; } } public static Result getResult(double[] array) { double max = Double.MIN_VALUE; double min = Double.MAX_VALUE; for (double i : array) { if (i>max) max = i; if (i<min) min = i; } return new Result(max, min); } }
|