可变长参数方法

public class varargsmethod {

	/**
	 * @param args
	 */
	public static double avg(double... values){
		double total = 0;
		int count = values.length;
		
		for(double i : values){
			total += i;
		}
		return total/count;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double[] dd = {12,1,1212};
		System.out.println(avg(3.14,23,45,5,2));
		System.out.println(avg(dd));
		System.out.println(String.valueOf(avg()));
		System.out.printf("平均数是 %s %n",avg(1,2));
	}

}

你可能感兴趣的:(可变长参数)