使用数组和数组列表

时间:2014-02-25 22:52:30

标签: java arrays

我的项目是创建一个执行此操作的程序:

  

类型为double的数组的数组已经初始化了   测试一个班级的分数。课程讲师希望最高   得分,最低分和所有等级的平均值。

编译以下代码时出现以下错误:

File: C:\Users\Guest\Downloads\grades.java [line: 16]
Error: Type mismatch: cannot convert from double to int
File: C:\Users\Guest\Downloads\grades.java [line: 23]
Error: grades cannot be resolved to a variable
File: C:\Users\Guest\Downloads\grades.java [line: 23]
Error: Type mismatch: cannot convert from double to int
 import java.util.Scanner;

public class grades
{
 // global variable declaration
 static Scanner cin = new Scanner(System.in);
 static final double NUM_GRADES = 5;

 public static void main(String[] args)
 {
      // declare variables
      double highestGrade = -999999;
      double lowestGrade = 999999;
      double sumOfGrades = 0;
      double avgGrades = 0;
      double[] scores = new double[NUM_GRADES]; // array is initialized using a final variable

      // use a for loop to obtain data from  user using the final variable
      for(double index=0; index < NUM_GRADES; ++index)
      {

          // this puts data into the current array index
           grades[index] = cin.nextDouble();

           // this calculates a running total of all the scores
           sumOfGrades += grades[index];

           // if current score in the array index is bigger than the current 'highestScore'
           // value, then set 'highestScore' equal to the current value in the array
           if(grades[index] > highestGrade)
           {
                highestGrade = grade[index];
           }

           // if current score in the array index is smaller than the current 'lowestScore'
           // value, then set 'lowestScore' equal to the current value in the array
           if(lowestGrade > grades[index])
           {
                lowestGrade = grades[index];
           }               
      }

      {
           System.out.print("\nThe grade for student #"+(index+1)+" is: "+grades[index]);
      }

      // display the highest/lowest numbers to the screen
      System.out.print("\n\nThese are the highest and lowest grades: ");
      System.out.print("\n\tHighest: "+ highestGrade);
      System.out.print("\n\tLowest: "+ lowestGrade);

 // find the average
 avgScores = sumOfGrades/NUM_GRADES;
 System.out.print("\nThe average score is: "+ avgGrades);

 // reset data back to 0 so we can find the ommitted average
 sumOfGrades = 0;
 avgGrades = 0;

}
}

6 个答案:

答案 0 :(得分:4)

对于数组大小,您只能在方括号int之间使用[]作为大小。将您的NUM_GRADES常量声明为int,而不是double

您声明了一个名为scores的双数组,但您没有使用它,然后您引用了不存在的grades。要么将所有grades引用更改为scores,要么将scores的名称更改为grades

答案 1 :(得分:3)

  • Error: Type mismatch: cannot convert from double to int - 您正在使用double作为大小初始化数组,这是不允许的,将NUM_GRADES类型更改为int:

    static final int NUM_GRADES = 5;
    
  • Error: grades cannot be resolved to a variable - 你没有一个名为grade的变量,这就是编译器抱怨的原因;可能您的变量scores应该被称为grades

  • Error: Type mismatch: cannot convert from double to int - 您正在使用double来索引for loop中的数组,将其更改为int:

    for (int index = 0; index < NUM_GRADES; ++index)
    

编辑(更多):

  • 变量grade在这段代码中不存在:

    if(grades[index] > highestGrade)
    {
        highestGrade = grade[index]; //<------- change it to grades
    }
    
  • 你的print()语句在for loop之外,因此无法访问索引变量;将它移到循环中

  • avgScores也没有被声明为变量,它应该是avgGrades,你在开头声明了

答案 2 :(得分:0)

对于“无法从double转换为int”错误,您可以通过强制转换为int来强制执行转换,例如grades[index] = (int)cin.nextDouble();但是这相当于从双倍中“截掉”小数位并最终得到一个基本上是“向下舍入”的int,如果这有意义的话。

对于“成绩无法解析为变量”错误“成绩”尚未被声明为变量(如得分)。

答案 3 :(得分:0)

NUM_GRADE是一个双倍。您不能将双精度数用作数组的索引。尝试将其声明为int,如下所示:

static final int NUM_GRADES = 5;

你在index犯了同样的错误。将其声明为int。

grades不存在。您可能打算使用scores

答案 4 :(得分:0)

修复编译时错误。

import java.util.Scanner;

public class Grade //changed here
{
 // global variable declaration
 static Scanner cin = new Scanner(System.in);
 static final int NUM_GRADES = 5;//changed here

 public static void main(String[] args)
 {
  // declare variables
  double highestGrade = -999999;
  double lowestGrade = 999999;
  double sumOfGrades = 0;
  double avgGrades = 0;
  double[] scores = new double[NUM_GRADES]; // array is initialized using a final variable

  // use a for loop to obtain data from  user using the final variable
  for(int index=0; index < NUM_GRADES; ++index)//changed here
  {

      // this puts data into the current array index
       //changed here
       scores[index] = cin.nextDouble();

       // this calculates a running total of all the scores
       sumOfGrades += scores[index];//changed here

       // if current score in the array index is bigger than the current 'highestScore'
       // value, then set 'highestScore' equal to the current value in the array
       if(scores[index] > highestGrade) //changed here
       {
            highestGrade = scores[index]; //changed here
       }

       // if current score in the array index is smaller than the current 'lowestScore'
       // value, then set 'lowestScore' equal to the current value in the array
       if(lowestGrade > scores[index])
       {
            lowestGrade = scores[index];//changed here
       }               
  }

  {
       System.out.print("\nThe grade for student #"+(index+1)+" is: "+scores[index]);//changed here
  }

  // display the highest/lowest numbers to the screen
  System.out.print("\n\nThese are the highest and lowest grades: ");
  System.out.print("\n\tHighest: "+ highestGrade);
  System.out.print("\n\tLowest: "+ lowestGrade);

 // find the average
 avgScores = sumOfGrades/NUM_GRADES;
 System.out.print("\nThe average score is: "+ avgGrades);

 // reset data back to 0 so we can find the ommitted average
 sumOfGrades = 0;
 avgGrades = 0;

}
}

答案 5 :(得分:0)

if(lowestGrade > grades[index])

...

System.out.print("\nThe grade for student #"+(index+1)+" is: "+grades[index]);

'index'需要是一个int(或转换为它)才能成为数组索引。

这应该可以帮到你......

for(int index=0; index < NUM_GRADES; ++index)