求数组中各元素的和
class HanShu05
{
public static void main(String[] args)
{
int[] arr={2,4,5,6,8,9,0,1};
int sum=0;
for (int x=0;x<arr.length ;x++ )
{
sum+=arr[x];
}
System.out.println(sum);
}
}
上面运行结果是35;
下面是想定义一个函数,求数组的和,但是出问题了
class HanShu05
{
public static void main(String[] args)
{
int[] arr={2,4,5,6,8,9,0,1};
int z=getSum();
System.out.println(z);
}
public static int getSum()
{
int sum=0;
for (int x=0;x<arr.length ;x++ )
{
sum+=arr[x]; 运行后此行错误:找不到符号
}
return sum;
}
}
求指点一下,谢谢~
|
|