public class Kebiancanshu {
public static void main(String[] args) {
System.out.println(average(8, 2, 3, 4, 5, 6, 7, 8, 1));
System.out.println(average(3,5,6,7));
}
public static double average(int n,int... nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
return (double)sum / nums.length;
}
}
n表示可变参数的个数,个数不确定的情况下,average参数可以只有可变参数数组,average(int...nums).
JAVA1.5版本以后支持可变参数。