JAVA基础(获取数组中的最值)

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载,违者必究。 https://blog.csdn.net/Cricket_7/article/details/91411451

1,数组获取最值(获取数组中的最大值最小值)

  • 思路:默认把arr[0]定义为最大值,然后依次去和arr[0]比较当值比较大时,进行记录

class Array {

    public static void main(String[] args) {

        int[] arr = {33,77,22,44,55};

        int max = getMax(arr);

        System.out.println(max);

    }

    /*

    获取数组中最大值

    1,返回值类型int

    2,参数列表int[] arr

    */

    public static int getMax(int[] arr) {

        int max = arr[0];

        for (int i = 1;i < arr.length ;i++ ) {            //从数组的第二个元素开始遍历

            if (max < arr[i]) {                            //如果max记录的值小于的数组中的元素

                max = arr[i];                            //max记录住较大的

            }

        }

        return max;

    }

}

猜你喜欢

转载自blog.csdn.net/Cricket_7/article/details/91411451