1

我收到一个错误,无法将 int 转换为 int[],有人可以帮忙吗?

//Create array
int [][] studentResults = new int [numStudents][numExams];

//Fill 1st dimension with Student numbers 1 through numStudents
for (int count = 0; count < numStudents; count++)
    studentResults[count][] = count + 1;
4

2 回答 2

1

在 Java 中,如果要为数组中的条目赋值,则需要指定数组的所有实例。我建议如下:

//Create array
int [][] studentResults = new int [numStudents][numExams];

//This loops through the two dimensional array that you created 
//And fills the 1st dimension with Student numbers 1 through numStudents.
for (int count = 0; count < numStudents; count++)
    for (int exam = 0; exam < numExams; exam++)
        studentResults[count][exam] = count + 1;

从而遍历studentResults每个学生的每个考试条目。

于 2013-06-25T02:13:01.317 回答
1

所以你需要设置每一行第一列的值。我们知道第一列索引是 0。所以对于每一行设置数组的 0 列,如下所示

for (int count = 0; count < numStudents; count++)
    studentResults[count][0] = count + 1;
于 2013-06-25T02:18:57.210 回答