# 請問誰能幫我看看我Java的程式碼哪裡寫錯了...

(1)在main method中，使用者輸入M與N；接著輸入每個學生N科總分，學生總成績必須介於0~100*N，錯誤輸入須顯示錯誤訊息，並重新輸入。呼叫下述method完成程式。例如M=3，N=2，為全班共3位學生，總共考兩科：接著輸入3位學生2科總分：{180,199,110}。

(2)寫一個method計算每一位學生平均分數。傳入成績陣列與科目數N，回傳平均分數。例如M=3，N=2：input:{180,199,110}, output:{90,99,55}。

(3)寫一個method計算平均及格的學生人數，傳入成績陣列與科目數，回傳平均及格人數。例如：M=3，N=2：input:{180,199,110}, output:2。

(4)寫一個method計算全班學生的平均分數後回傳。例如：M=3，N=2：input:{180,199,110}, output:81.3。

http://www.wretch.cc/blog/frank0125

at Test2.average(Test2.java:45)

at Test2.main(Test2.java:26)

Update:

P.S.

Update 2:

Update 3:

(少一個陣列XD)

Update 4:

Update 5:

for (int index = 0; index < b.length←這裡; index++)

THX~^M^~y

Update 6:

public static int pass(int z, int[] b)

public static double[] average(int z, int[] b)

arrayTwo = pass(y, a);

public static int pass(int z, int[] b)

Update 7:

array = average(y, a);

Rating

import java.util.*;

public class Test2 {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

int M, N;//M:人數 , N:科目數

int[] score;//每個學生N科總分

System.out.println("Please enter how many students are there?");

M = keyboard.nextInt();

System.out.println("Please enter how many subjects are there?");

N = keyboard.nextInt();

System.out.println("Please enter the total subjects score for each students:");

score = new int[M];

for (int index = 0; index < M; index++) {

score[index] = keyboard.nextInt();

while(score[index] < 0 || score[index] > N*100) {

System.out.println("The scores you enter cannot below 0 or above 100!");

score[index] = keyboard.nextInt();

}

}

double[] array;

array = average(N, score);

int arrayTwo;

arrayTwo = pass(N, score);

double arrayThree;

arrayThree = averageTwo(M, N, score);

System.out.print("The average scores of each students are:");

for (int index = 0; index < M; index++) {

System.out.print(" " + array[index]);

}

System.out.println();

System.out.println("The number of students whose score is over 60 is: " + arrayTwo);

System.out.println("The average score of the whole class is: " + arrayThree);

}

public static double[] average(int N, int[] score) {

double[] averagePerson = new double[score.length];

for (int index = 0; index < score.length; index++) {

averagePerson[index] = score[index] / N;

}

return averagePerson;

}

public static int pass(int N, int[] score) {

int count = 0;

for (int index = 0; index < score.length; index++) {

if (score[index] / N > 60) {

count++;

}

}

return count;

}

public static double averageTwo(int M,int N, int[] score) {

int sum = 0;

double averageClass;

for (int index = 0; index < score.length; index++) {

sum += score[index];

}

averageClass = (double)sum / (double)(M*N);

return averageClass;

}

}

2006-01-25 18:12:43 補充：

你這程式會出現陣列索引值超出範圍

所以我把函數裡所有的變數都寫成你題目中的M、N和score

免得讓你給搞混掉

幫變數命名也是很重要的喔

只是老師們常常會跳過....

Source(s): 剛剛寫的
• double[] averagePerson = new double[z];

averagePerson[z] = b[index]/z;

以上這2行肯定出錯,你原本是不要要寫成

averagePerson[index] = b[index]/z;

...?

2006-01-25 13:34:09 補充：

問題點:

int[] a = new int[x];

array = average(y, a);

你確定x會等於y嗎..?