2014-02-25 38 views
0

我的項目是創建一個程序,這是否:使用數組和數組列表

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; 

} 
} 
+3

難道你不明白什麼這些錯誤?他們似乎不言自明。 –

+0

改爲使用0.0或999.0並查看?除此之外,我沒有看到任何錯誤。 – Mukus

+1

雙精度浮點數。 'NUM_GRADES'是一個雙。它可能有一個像3.2或42.01的值。具有多個元素的數組看起來像什麼?數組的大小(也就是'NUM_GRADES')需要是一個整數。 –

回答

4

您只能使用一個int的大小括號之間[]一個數組的大小。聲明您的NUM_GRADES常數爲int,而不是double

您聲明瞭一個名爲scores的雙數組,但您不使用它,然後您參考grades哪些不存在。請將所有grades引用文件更改爲scores或將scores的名稱更改爲grades

0

對於「無法從double轉換爲int」錯誤,您可以強制轉換爲轉換爲int grades[index] = (int)cin.nextDouble();,但這相當於從double中「剔除」小數位,並且最終得到的int基本上是「向下舍入」的,如果有意義的話。

對於「等級無法解析爲變量」的錯誤,「等級」沒有被聲明爲任何變量(如分數)。

+0

請注意,我不是在告訴你,你應該投出雙倍,但你可以。我認爲更合適的解決方案是使用正確的類型。 – KyleM

0

NUM_GRADE是雙精度型。您不能將雙打用作數組的索引。嘗試聲明爲int,而不是像這樣:

static final int NUM_GRADES = 5;

您與index犯同樣的錯誤。將其聲明爲int。

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

3
  • Error: Type mismatch: cannot convert from double to int - 要初始化採用雙層的大小,這是不允許的數組,改變NUM_GRADES類型爲int:

    static final int NUM_GRADES = 5; 
    
  • Error: grades cannot be resolved to a variable - 你沒有一個檔次變量稱爲,這就是編譯器抱怨的原因;可能是你的變量scores應該叫grades

  • Error: Type mismatch: cannot convert from double to int - 你正在使用雙索引的陣列for loop,將其更改爲int:

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

EDIT(幾個):

  • 變量grade在此piec中不存在代碼E:

    if(grades[index] > highestGrade) 
    { 
        highestGrade = grade[index]; //<------- change it to grades 
    } 
    
  • 打印()語句外for loop,所以索引變量將無法訪問;移動它的循環

  • avgScores也沒有聲明爲變量中,這也許應該是avgGrades,你在一開始

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; 

} 
} 
+0

這仍然不能編譯,因爲NUM_GRADES是double類型的,並且用作數組大小:)順便說一句,如果您對已更改內容的評論更具可見性,那將會非常有用 – Melquiades

+0

是的。沒有看到。 – Mukus

+0

這仍然不會編譯,因爲在for循環中,索引是double類型的,用於訪問數組索引:) – Melquiades

0
if(lowestGrade > grades[index]) 

...

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

'索引' 將需要一個int(或澆鑄到它)是一個數組索引。

這應該可以解決你......

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