调用方法求数组中最大值

先熟悉找3个值中的最大值
package com.fqs.demo;

public class ChongZ {
    //求3个数的最大值 并显示
    public static void main(String[] args) {
        int a=3;
        int b=37;
        int c=19;
        int max=a;//假定a是最大值max
        if(max<b) {//如果max<b 将比较大的b 赋值给max  当数组时需要切换为数组[下标]
            max=b;
        };
        if(max<c) {//如果max<c 将比较大的c 赋值给max
            max=c;
        }
    System.out.println("max:"+max);//输出最大值
        
    }        
}
        
    

 

package com.fqs.demo;

public class ChongZ {
    //求数组中的最大值
    public static void main(String[] args) {
        getMax(2,1,3);
    }
    public static void    getMax(int a,int b,int c) {
        
    int array[]= {a,b,c};
        int max=array[0];
        //下标0已结给了假定的最大值;对比的数下标从1开始,范围小于3,所以取值=1,2
        for(int index=1;index<array.length;index++) {
            if(max<array[index]) {
                max=array[index];
            }
        }
        
        System.out.println("max:"+max);
    }        
}
        
    

 


 

posted @ 2023-01-14 22:31  胖豆芽  阅读(80)  评论(0编辑  收藏  举报