可变参数列表

时间:2019-06-09 22:26:27

标签: java

无论输入什么,我的代码都会始终返回零。我在做什么错了?

public class VariableArgumentList {

public static void main(String[] args) {
    System.out.println(product(2,2,2));

}
public static int product(int...integers) {//method product using an elipse
    int product = 0;//set product to zero
    for (int x:integers)//enhanced for loop, sets integer to x that takes integers
        product *= x;//loop through entered integers, multiply them and store product in variable x

    return product*integers.length;//setting return for method product
    }
}

我希望输出为2 * 2 * 2 =8。我得到的输出为零。

1 个答案:

答案 0 :(得分:0)

public static void main(String[] args)
    {
        System.out.println(product(2, 2, 2, 2));

    }

    public static int product(int... integers)
    {// method product using an elipse
        int product = 1;// set product to zero
        for (int x : integers)// enhanced for loop, sets integer to x that takes integers
            product *= x;// loop through entered integers, multiply them and store product in variable x

        return product ;// setting return for method product
    }