我想用Java编写,使得其可消耗物品的施氮量,返回它们的平均值的平均方法:java的可选参数
我的想法是:
public static int average(int[] args){
int total = 0;
for(int i=0;i<args.length;i++){
total = total + args[i];
}
return Math.round (total/args.length);
}
//test it
average(1,2,3) // s**hould return 2.
我如何改变我的方法消耗任何数量的参数,而不是int []参数,所以可以按我想要的方式工作? 干杯
可能的重复:http://stackoverflow.com/questions/965690/java-optional-parameters –