我不是一個get程序員,我一直有我的代碼問題。 我得到的錯誤:數組混淆
`java.lang.ArrayIndexOutOfBoundsException: 0`
一些幫助將不勝感激。謝謝。
我的代碼:
import java.util.Scanner;
public class Gradebook1D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] id = new int[0];
double[] quiz1 = new double[0];
double[] quiz2 = new double[0];
double[] quiz3 = new double[0];
int idStudent = 0;
System.out.print("Enter how student's are in your class: ");
idStudent = input.nextInt();
double[] sum = new double[0];
for (int i = 0; i < idStudent; i++){
System.out.println("Enter student ID: ");
id[i] = input.nextInt();
System.out.print("\n Enter score for Quiz1: ");
quiz1[i] = input.nextInt();
System.out.print("\n Enter score for Quiz2: ");
quiz2[i] = input.nextInt();
System.out.print("\n Enter score for Quiz3: ");
quiz3[i] = input.nextInt();
sum[i] = quiz1[i] + quiz2[i] + quiz3[i];
}
}
}
'new int [0]','new double [0]'表示零大小! – Baby
謝謝大家的幫助。我不知道我必須先獲取輸入然後初始化數組。 – SkyToken