Java基础系列

package com.test6;

public class test5 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 31, 4, 5, 6, 7, 88, 9, 11, -1};
        float[] arr2 = {1, 2, 31, 4, 5, 6, 7, 88.88f, 9, 11.1f, -1f};
        ArrayHelper ah = new ArrayHelper();
        ah.GetMinAndMax(arr);
        ah.GetMinAndMax(arr2);
        /** 打印显示
         数组元素包括:1 2 31 4 5 6 7 88 9 11 -1
         数组的最大值是:88
         数组的最小值是:-1
         数组元素包括:1.0 2.0 31.0 4.0 5.0 6.0 7.0 88.88 9.0 11.1 -1.0
         数组的最大值是:88.88
         数组的最小值是:-1.0
         */
    }
}

class ArrayHelper {
    public void GetMinAndMax(int[] arr) {
        int min = arr[0];
        int max = arr[0];
        System.out.print("数组元素包括:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
            if (arr[i] > max)   // 判断最大值
                max = arr[i];
            if (arr[i] < min)   // 判断最小值
                min = arr[i];
        }
        System.out.println();
        System.out.println("数组的最大值是:" + max); // 输出最大值
        System.out.println("数组的最小值是:" + min); // 输出最小值
    }

    public void GetMinAndMax(float[] arr) {
        float min = arr[0];
        float max = arr[0];
        System.out.print("数组元素包括:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
            if (arr[i] > max)   // 判断最大值
                max = arr[i];
            if (arr[i] < min)   // 判断最小值
                min = arr[i];
        }
        System.out.println();
        System.out.println("数组的最大值是:" + max); // 输出最大值
        System.out.println("数组的最小值是:" + min); // 输出最小值
    }
}

  

原文地址:https://www.cnblogs.com/smartsmile/p/11549360.html