调用可变参数的方法

package com.wuming.struct;

public class Demo04 {
    public static void main(String[] args) {
        //调用可变参数的方法
        printMax(34,3,4,56.7);
        printMax(new double[]{1,2,3});
    }
    public static void printMax(double... numbers){
        if (numbers.length==0){
            System.out.println("No argument passed");
            return;
        }
        double result=numbers[0];
        //排序
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i]>result){
                result=numbers[i];
            }
        }
        System.out.println("The max value is" + result);
    }
}

The max value is56.7
The max value is3.0

 

おすすめ

転載: blog.csdn.net/wanggang182007/article/details/121482706