int [] 不能转换为 int [英] int [] cannot be converted to int

查看:50
本文介绍了int [] 不能转换为 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];

但我收到一个错误 int[] 无法转换为 int.

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

推荐答案

您有:

gradebook = new int [numberOfStudents][examScores];

但是 examScores 是一个 int[].数组维度必须是 int,因此 examScores 不能用作数组维度(我可以理解您对错误的困惑,因为它是字面意思:它想要一个int 但你给了它一个 int[]).

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];

其中 numberOfExams 是您需要的 int,包含考试计数.不过只是猜测.

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

这篇关于int [] 不能转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆