Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

java - int [] cannot be converted to int

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...