2016-11-18 141 views
1

我正在自學Java,剛剛完成了數組的基礎知識學習,或者我認爲。我想創建一個能夠保證每個學生的成績和平均成績的班級成績單。我有一個for循環,要求10名學生中的每一個都要輸入4個測試成績。一旦我拿到這四個等級,我就取平均分。然後,我將學生的成績和平均數存入一個數組中,每個學生一個數組。我基本上用這5個元素創建了10個數組,每個學生使用for循環。我現在想從每個學生的數組中取第5個元素,即4個等級的平均值,並填充另一個稱爲平均值的數組,以便我可以執行其他計算。這可能與我的邏輯?我認爲我可以硬編碼10個陣列,每個學生1個像這樣:使用數組的元素填充另一個數組 - Java

double averages[] = {student1[4], student2[4], ..., student10[4]}; 

這不是一個糟糕的方式去解決這個問題嗎?任何建設性的幫助或指導將不勝感激。請不要發佈提供答案的代碼,因爲我不會從中學習。我只想要一個正確的方向提示。 :)

這裏是我的代碼,直到混亂的點:

import java.util.Scanner; 

public class ClassAverages { 

    public static void main(String[] args) { 

     Scanner keyboard = new Scanner(System.in); 

     double grade1 = 0.0, grade2 = 0.0, grade3 = 0.0, grade4 = 0.0, average = 0.0; 

     // get grades from each of the 10 students 
     for (int student = 1; student <= 3; student++) { 
      System.out.println("Student " + student); 
      System.out.println("---------\n"); 
      System.out.print("Enter the first grade: "); 
      grade1 = keyboard.nextDouble(); 
      while (grade1 < 0) { // input validation for grade 1 
       System.out.print("You entered a negative value for grade. Please re-enter a positive grade: "); 
       grade1 = keyboard.nextDouble(); 
      } 
      System.out.print("Enter the second grade: "); 
      grade2 = keyboard.nextDouble(); 
      while (grade2 < 0) { // input validation for grade 2 
       System.out.print("You entered a negative value for grade. Please re-enter a positive grade: "); 
       grade2 = keyboard.nextDouble(); 
      } 
      System.out.print("Enter the third grade: "); 
      grade3 = keyboard.nextDouble(); 
      while (grade3 < 0) { // input validation for grade 3 
       System.out.print("You entered a negative value for grade. Please re-enter a positive grade: "); 
       grade3 = keyboard.nextDouble(); 
      } 
      System.out.print("Enter the fourth grade: "); 
      grade4 = keyboard.nextDouble(); 
      System.out.println(); 
      while (grade4 < 0) { // input validation for grade 4 
       System.out.print("You entered a negative value for grade. Please re-enter a positive grade: "); 
       grade4 = keyboard.nextDouble(); 
       System.out.println(); 
      } 

      // calculate the current student's average 
      average = (grade1 + grade2 + grade3 + grade4)/4; 

      // for each student, 1 to 10, create an array with their 4 grades and average 
      double studentX[] = { grade1, grade2, grade3, grade4, average }; 
      System.out.println("SCORE 1\t\tSCORE 2\t\tSCORE 3\t\tSCORE 4\t\tAVERAGE"); 
      System.out.print(studentX[0] + "\t\t"); 
      System.out.print(studentX[1] + "\t\t"); 
      System.out.print(studentX[2] + "\t\t"); 
      System.out.print(studentX[3] + "\t\t"); 
      System.out.print(studentX[4] + "\n"); 
      System.out.println(); 

      // I want to use each student's average for each corresponding element in the averages array 
      // create an array of all student's averages 
      // double averages[] = {student1average, student2average,...student10average} ??? 
     } 
    } 
} 

輸出截至目前:

Student 1 
--------- 

Enter the first grade: 100 
Enter the second grade: 100 
Enter the third grade: 100 
Enter the fourth grade: 100 

SCORE 1  SCORE 2  SCORE 3  SCORE 4  AVERAGE 
100.0  100.0  100.0  100.0  100.0 

Student 2 
--------- 

Enter the first grade: 90 
Enter the second grade: 90 
Enter the third grade: 90 
Enter the fourth grade: 80 

SCORE 1  SCORE 2  SCORE 3  SCORE 4  AVERAGE 
90.0  90.0  90.0  80.0  87.5 

Student 3 
--------- 

Enter the first grade: 100 
Enter the second grade: 100 
Enter the third grade: 90 
Enter the fourth grade: 80 

SCORE 1  SCORE 2  SCORE 3  SCORE 4  AVERAGE 
100.0  100.0  90.0  80.0  92.5 

回答

1

你可以使用一個for循環。但是,這將需要你有一個項目集合迭代。我認爲你做這件事的最好方法是把你的學生組織到他們自己的單獨班級中(把班級的所有成績作爲參數加入到班級中,並在班級中進行平均計算)。

所以,如果你打電話給你的類student那麼你的構造函數可能看起來像這樣student(int grade1, int grade2, int grade3, int grade4)。使用這些參數來計算每個學生對象的平均值。

然後在您的ClassAverage類只實例化新student對象,只要你想,並把它們添加到student對象和數組,接着它的那樣簡單通過student數組迭代,提取您爲每個學生對象創建的average場(簡單地通過陳述studentName.average,假設你在學生課程中將average字段average命名)。

所以最後你應該看起來像這樣。我們假設學生對象的數組被稱爲studentArray

int[] averageArray = new int[studentArray.length]; 
for (int i = 0; i < studentArray.length; i++){ 
averageArray[i] = studentArray[i].average; 
} 

祝你好運!

+0

我與階級結構是方式達成一致來解決這個問題。但是,因爲他剛剛學過數組,所以我不確定這是否屬於他所問的問題的範圍。我可以這樣寫: – KyleKW

+0

in java 8它可以是: 'int [] averageArray = Stream.of(studentArray).map(student - > student.average).toArray();' – maxpovver

+0

我同意這可能有點複雜,但我有意識到教compsci最好的方法之一是遇到一些困難的概念,並花費大量的時間來解決它。你不好意思將自己從可能困難的事情中解脫出來!不過,我完全同意這對於某人出發可能有些複雜。 – Jay

1

數組是存儲信息的非常糟糕的形式。主要是因爲它們不包含使用此數組內的數據的邏輯,並且任何其他代碼都可能使該數據無效,因爲數組不檢查您給出的值。這是什麼類的用途:使數據和代碼與它一起工作,並保護數據免受代碼中其他地方的變化。

因此,對於你的情況數組可以爲相同類型的字段(例如,學生數組是OK),但不適用於該類型的值(學生成績數組數組不成問題)。所以,你應該在這裏做的是使用一個類:

class Student 
{ 
    int grades[] = new int[4]; 
    public Student(int grade1, grade2, int grade3, int grade4) 
    { 
      grades[0] = grade1; 
      grades[1]=grade2; 
      grades[2]=grade3; 
      grades[3]=grade4; 
    } 
    public double average() 
    { 
     double result = 0; 
     for(int i =0; i<grades.count; i++) 
      result += grades; 
     return result/grades/count; 
     } 
    // and so on 
} 

他們讓你把裏面的對象邏輯,SOU你可以使用它像這樣:

Student albert = new Student(5, 4, 2, 1); 
System.out.println(albert.avarage()); // prints 3 
相關問題