public class T12 {
public static double sumArray(double[] array){
static double sum = 0;
int i;
for(i=0;i<array.length;i++)
sum += array[i];
return sum;
}
public static void main(String[] args) {
double[] array = {1,2,3,4,5,6,7,8,9};
double a;
a = T12.sumArray(double[] array);
System.out.println(a);
}
}
上面代碼有什麼問題嗎,為什麼不能運行?
public class T12 {
public static double sumArray(double[] array){
static double sum = 0; //這裡改成 double sum = 0;,局部變量不能定義static
int i;
for(i=0;i<array.length;i++)
sum += array[i];
return sum;
}
public static void main(String[] args) {
double[] array = {1,2,3,4,5,6,7,8,9};
double a;
a = T12.sumArray(double[] array); //去掉double[] ,傳參,不需要數組定義
System.out.println(a);
}
}