【问题标题】:int [] cannot be converted to intint [] 不能转换为 int
【发布时间】:2014-06-04 08:37:15
【问题描述】:

我想将二维数组用于成绩簿方法,其中包含学生姓名,然后是考试成绩。在课堂上我有如下。

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[] cannot be convert to int.

【问题讨论】:

  • 那么,你认为这个错误意味着什么?
  • 这里还有一个严重的问题。您正在创建大小为零的数组。在创建数组studentNameexamScoresgradebook 之前,您确实需要将numberOfStudentsnumberOfExams(或类似的值)设置为非零值;否则这些数组将没有空间容纳任何实际值。

标签: java arrays class


【解决方案1】:

你有:

gradebook = new int [numberOfStudents][examScores];

但是examScoresint[]。数组维度必须是 int,所以 examScores 不能用作数组维度(我可以理解您对错误的困惑,考虑到它的字面意思:它想要一个 int 但你给了它一个 @987654327 @)。

从你的描述来看,我猜你的意思更像是:

gradebook = new int [numberOfStudents][numberOfExams];

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

【讨论】:

  • 非常感谢杰森!非常有用的信息并将其添加到我的笔记本中,甚至哈哈。很高兴你理解我的困惑。你帮了大忙!
【解决方案2】:

examScores 是一个 int[] 数组,数组大小应该是int,所以它会给你一个错误。

【讨论】:

    【解决方案3】:

    它准确地告诉你问题是什么。

    examScores 是一个 int array,但您正尝试将其用作单个整数来确定您希望数组 gradebook 的第二维有多大。

    您可能希望使用examScores.length 或某个数字来指示您希望每个学生获得多少考试成绩。

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多