java 求整形数组的各个元素的和,用下面两种方法如何实现呢
发布网友
发布时间:2022-04-29 01:43
我来回答
共1个回答
热心网友
时间:2022-06-27 20:51
package test;
public class Test
{
public static void main(String[] args)
{
int[] arr = new int[]{2,3,4,5,6,7,8};
System.out.println(Sum.sum(arr));
System.out.println(Sum2.sum(arr));
}
private static class Sum2{
public static int sum(int[] args){
int sum = 0;
for (int i : args)
{
sum += i;
}
return sum;
}
}
}
class Sum {
public static int sum(int[] args){
int sum = 0;
for (int i : args)
{
sum += i;
}
return sum;
}
}