[問題] 計算Array的平均值
我想寫一個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的錯誤
我是初學者看不出問題出在哪 :(
麻煩版上的強者解答了~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.166.131.169
※ 文章網址: https://www.ptt.cc/bbs/java/M.1505486084.A.7B1.html
→
09/15 22:44, , 1F
09/15 22:44, 1F
→
09/15 22:45, , 2F
09/15 22:45, 2F
→
09/15 22:47, , 3F
09/15 22:47, 3F
→
09/15 22:47, , 4F
09/15 22:47, 4F
→
09/15 22:48, , 5F
09/15 22:48, 5F
→
09/16 18:28, , 6F
09/16 18:28, 6F
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章