0
我想用sum方法獲得數組的總和,但是它在最後顯示出「返回結果」部分的「不兼容類型」。我試圖找出如何修復它,但我有點卡住了。使用sum方法獲得數組的總和
public class Prog2
{
public Prog2()
{
int a[] = {7, 8, 9, 9, 8, 7};
System.out.println(sum(a));
}
public int[] sum(int s[])
{
int result = 0;
for (int i = 0; i < s.length; i ++)
{
result += s [i];
}
return result;
}
}
看看你試圖返回的結果的類型,你說你會返回的類型(在方法聲明中) - 它們是否相同? – panini 2014-10-19 19:52:34