2016-10-10 53 views
-4

需要編寫程序的幫助。我無法弄清楚如何添加到特定的數組中。以下是我必須編寫的程序。任何幫助,將不勝感激。用於跟蹤字母等級的數組

編寫將使用一維數組的應用程序。詢問用戶一系列數字等級(等級-1將結束輸入)。創建一個一維數組以跟蹤等級範圍。例如:

索引0表示在90〜年級 - 100 指數1表示在80的範圍內年級 - 89 索引2表示在70〜年級 - 79 索引3表示範圍等級60 - 69 索引4表示等級在59或更低的範圍內

顯示A,B,C,D和F的數量;還顯示平均分,最高分和最低分。 這裏是你的程序應該是什麼樣子:

輸入一組數字等級(0-100)或-1退出:90

輸入一組數字等級(0-100)或-1退出:82

輸入一組數字等級(0-100)或-1退出:96

輸入一組數字等級(0-100)或-1退出:-1

A級的數量: 2

數B等級:1

Ç等級的數:0

d等級數:0

˚F等級的數:0

平均是:89.33

最高等級是:96

最低價等級:82

回答

0

我知道有一個更好的方法來做到這一點,比如將問題分解成單獨的方法,從而使您的代碼更易於調試/維護,但這是我在不使用任何方法的情況下完成的。

public static void main(String[] args) { 
    final int SIZE = 100; 
    int[] array = new int[SIZE]; 
    int count = 0; 
    int ans; 
    int[] gradeRange = {0, 0, 0, 0, 0}; 
    double total = 0; 
    double average = 0; 
    int highest = array[0]; 




    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Enter a numeric grade (0-100) or -1 to quit:"); 
    ans = keyboard.nextInt(); 

    while (ans != -1 && count < array.length) { 
     array[count] = ans; 
     count++; //counter used to keep track of the array length 

     if (ans >= 90) { 
      gradeRange[0]++; 

     } 
     else if (ans >= 80) { 
      gradeRange[1]++; 

     } 
     else if (ans >= 70) { 
      gradeRange[2]++; 

     } 
     else if (ans >= 60) { 
      gradeRange[3]++; 

     } 
     else if (ans < 60) { 
      gradeRange[4]++; 

     } 

     System.out.println("Enter a numeric grade (0-100) or -1 to quit:"); 
     ans = keyboard.nextInt(); 
    } 

    int[] secarray = new int[count]; 
    for (int index = 0; index < count; index++) { 
     secarray[index] = array[index]; 
    } 
    int min = secarray[0]; 



    for (int index = 0; index < count; index++) { 
     total += array[index]; 
     average = total/count; 
    } 


    for (int index = 0; index < count; index++) { 
     if (array[index] > highest) 
      highest = array[index]; 

    } 
    for (int index = 1; index < secarray.length; index++) { 

     if (secarray[index] < min) 
      min = secarray[index]; 
    } 







    System.out.println("Number of A grades: " + gradeRange[0]); 
    System.out.println("Number of B grades: " + gradeRange[1]); 
    System.out.println("Number of C grades: " + gradeRange[2]); 
    System.out.println("Number of D grades: " + gradeRange[3]); 
    System.out.println("Number of F grades: " + gradeRange[4]); 

    System.out.printf("Average is: %.1f\n ", average); 
    System.out.println("Lowest grade is: " + min); 
    System.out.println("Highest grade is: " + highest); 


}//end main