我想写一个array的constructor还有一个average的method去计算其平均值
程式码如下:
public class Array2 {
public int sum;
public int x, r0, r1, r2, r3, r4, r5;
public int num[];
public Array2(int r0, int r1, int r2, int r3, int r4, int r5) {
num = new int[]{this.r0, this.r1, this.r2, this.r3, this.r4, this.r5};
}
public double average() {
for (int x : num) {
sum += x;
}
return sum / num.length;
}
class main {
public static void main(String args[]) {
Array2 arr = new Array2(2, 4, 6, 8, 10, 12);
arr.average();
System.out.println(arr.average());
}
}
}
但执行后会出现inner class cannot have static declaration的错误
我是初学者看不出问题出在哪 :(
麻烦版上的强者解答了~