手记

静态类求最大值最小值

package example;

public 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;
            }
            else
                min=i;
        }
        return new Result(max,min);
    }
    public static void main(String[] args) {
        double array[]=new double[5];
        for (int i = 0; i < array.length; i++) {
            array[i]=100*Math.random();
        }
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println(MaxMin.getResult(array).getMax());
    }
}
0人推荐
随时随地看视频
慕课网APP