我的項目是創建一個程序,這是否:使用數組和數組列表
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;
}
}
難道你不明白什麼這些錯誤?他們似乎不言自明。 –
改爲使用0.0或999.0並查看?除此之外,我沒有看到任何錯誤。 – Mukus
雙精度浮點數。 'NUM_GRADES'是一個雙。它可能有一個像3.2或42.01的值。具有多個元素的數組看起來像什麼?數組的大小(也就是'NUM_GRADES')需要是一個整數。 –