int [] cannot be converted to int

2019-03-07 02:37发布

I want to use a 2D array for a gradebook method, where it has the student names, then the exam scores. In the class I have as follows.

private int numberOfStudents;    
private String [] studentName;
private int  examNumber;
private int [] examScores;
private int [][] gradebook;

public ExamAverage ()
{
numberOfStudents = 0;
studentName = new String[numberOfStudents];
examNumber = 0;
examScores = new int [numberOfStudents]; 
gradebook = new int [numberOfStudents][examScores];

Yet I get an error int[] cannot be converted to int.

3条回答
时光不老,我们不散
2楼-- · 2019-03-07 03:07

It's telling you exactly what the problem is.

examScores is an int array but you're trying to use it as a single integer for how large you want the 2nd dimension of your array gradebook to be.

You probably want to use examScores.length or some number that indicates how many exam scores per student you want.

查看更多
We Are One
3楼-- · 2019-03-07 03:08

examScores is an int[] array, and array size should be int so it gives you an error.

查看更多
SAY GOODBYE
4楼-- · 2019-03-07 03:28

You have:

gradebook = new int [numberOfStudents][examScores];

But examScores is an int[]. Array dimensions have to be an int, so examScores cannot be used as an array dimension (I can understand your confusion about the error, given how literal it is: it wants an int but you gave it an int[]).

Judging by your description I'm guessing you mean something more along the lines of:

gradebook = new int [numberOfStudents][numberOfExams];

Where numberOfExams is an int you'll need, containing the count of exams. Just a guess, though.

查看更多
登录 后发表回答