无论输入什么,我的代码都会始终返回零。我在做什么错了?
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。我得到的输出为零。
答案 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
}