public class Demo {
public static void main(String[] args) {
//可变参数
System.out.println(sum(43,43,25,3214,22,54,1,343,654,324,2135,436,3,532,15,476,35,3254,367,53,53,6,546,425,43));
System.out.println(sum(10,20));//两个参数
System.out.println(sum(10,20,30));//可变参数
System.out.println(sum(10,20,30,40,50));//五个参数
System.out.println(sum());//可变参数
}
//求两个数的累加和
public static int sum(int a ,int b){
return a + b;
}
//求五个数的累加和
public static int sum(int a ,int b ,int c ,int d ,int e){
return a + b + c + d + e;
}
//
/*public static int sum(int[] arr){
int sum = 0;
for(int a : arr){
sum += a;
}
return sum;
}*/
//可变参数
public static int sum(int ... arr){
int sum = 0;
for(int a : arr){
sum += a;
}
return sum;
}
} |