$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n"; //==20
我怎么能在java中做到这一点?
答案 0 :(得分:5)
long array_sum(int...array)
{
long sum = 0;
for(int value : array)
sum += value;
return sum;
}
示例:强>
System.out.println(array_sum(1, 2, 3, -1));
System.out.println(array_sum(1, 2));
System.out.println(array_sum(new int[]{1,2,3,4,5}));
5
3
15
答案 1 :(得分:0)