java - 使用数组和数组列表

标签 java arrays

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

The array of type double called grades has been initialized with the test scores for a class. The course instructor would like the highest score, the lowest score, and the mean value of all the grades.

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

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;

}
}

最佳答案

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

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

关于java - 使用数组和数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22027984/

相关文章:

java - 尝试返回一个字符串数组

php - 通过 PHP 的 CSS 下拉菜单,逻辑打败了我

ios - 使用高阶 Swift 函数的潜在内存泄漏

java - Git 克隆失败

java - 将 Vaadin7 StreamResource 保存到文件

java - 具有多个主类的 Gradle 应用程序插件

java - NullPointerException 错误,将 string[] 转换为 int[]

java - 两个正整数乘以long的结果为负值

java - Apache Kafka : seek and assignment. 从头可靠读取

JavaScript 从数组中删除元素直到 x