2014-03-26 178 views
-2
package edu.tests.model; 

    /** 
    * TestResult represents the grade on a test for one student. 
    * 
    * @author 
    * @version 
    */ 
    public class TestResult { 

     private String id; 
     private int grade; 

     /** 
     * Creates a new instance of TestResult with the specified 
     * ID of the student who took the test and the grade the 
     * student earned. 
     * 
     * @requires id != null && id.length() > 0 && grade >= 0 
     * @ensures getId().equals(id) && getGrade() == grade 
     * 
     * @param id the student's ID 
     * @param grade the student's grade on the test 
     */ 
     public TestResult(String id, int grade) { 
      this.id = id; 
      this.grade = grade; 
     } 

     /** 
     * Returns the ID of the student. 
     * 
     * @requires nothing 
     * 
     * @return the student's ID 
     */ 
     public String getId() { 
      return this.id; 
     } 

     /** 
     * Returns the grade earned by the student. 
     * 
     * @requires nothing 
     * 
     * @return the student's test grade 
     */ 
     public int getGrade() { 
      return this.grade; 
     } 


     /** 
     * Returns true if the specified object is a TestResult instance 
     * with the same student ID, and false otherwise. 
     * 
     * @requires nothing 
     * 
     * @return true iff someObject is a TestResult with the same student ID 
     */ 
     @Override 
     public boolean equals(Object someObject) { 
      if (this == someObject) { 
       return true; 
      } 
      if (someObject == null) { 
       return false; 
      } 
      if (this.getClass() != someObject.getClass()) { 
       return false; 
      } 
      TestResult aTestResult = (TestResult) someObject; 

      return this.getId().equals(aTestResult.getId()); 
     } 

    } 

package edu.westga.tests.model; 

import java.util.ArrayList; 

/** 
* Gradebook represents the test results for all students who took a test. 
* 
* @author 
* @version 
*/ 
public class Gradebook { 

    // TODO #1: declare an instance variable called results, which is an 
    // ArrayList of TestResult objects 
    ArrayList<TestResult> results; 

    /** 
    * Creates a new Gradebook object with no test results. 
    * 
    * @requires nothing 
    * @ensures size() = 0 
    */ 
    // TODO#2: implement a constructor with 0 parameters that 
    // initializes the instance variable as an empty ArrayList 
    public Gradebook() { 
     this.results = new ArrayList<TestResult>(); 
    } 

    /** 
    * Returns the number of test results that have been added to this 
    * Gradebook. 
    * 
    * @requires nothing 
    * 
    * @return how many test results this Gradebook records 
    */ 
    // TODO#3: implement the method size, which returns an int and takes 
    // 0 parameter 
    public int size() { 
     return this.results.size(); 

    } 

    /** 
    * Adds the specified test result to this Gradebook. 
    * 
    * @requires aResult != null && !contains(aResult) 
    * @ensures size() == size()@prev + 1 && contains(aResult) 
    * 
    * @param aResult 
    *   the test result to add to this Gradebook 
    */ 
    // TODO#4: implement the method add, with 1 parameter called aResult of type 
    // TestResult 
    // Please read the comments for the method to understand what it does 
    public int add(TestResult aResult) { 
     return this.add(aResult); 
    } 

    /** 
    * Returns true if a TestResult with the same ID as the specifed test result 
    * has previously been added to this Gradebook, and false otherwise. 
    * 
    * @requires aResult != null 
    * 
    * @return true iff the aResult has the same ID as a TestResult that has 
    *   already been added 
    * 
    * @param aResult 
    *   the test result to check 
    */ 
    // TODO#5: implement the method contains, which returns a boolean 
    // and takes 1 parameter of type TestResult 
    public boolean contains(TestResult aResult) { 
     return this.contains(aResult); 
    } 

    /** 
    * Returns the average grade of the test results that have been added to 
    * this Gradebook, or 0 if none have been added. 
    * 
    * @requires nothing 
    * 
    * @return the mean grade 
    */ 
    // TODO#6: implement method averageGrade, which returns an int 
    // and takes 0 parameters 

    public int averageGrade() { 
     for (int i = 0; i < results.size(); i++); 

     { 
      TestResult theResult = results.get(i); 
      int averageGrade = theResult.getGrade(); 

     } 

    } 

} 

嘿大家好,我正在嘗試編寫for-each循環,它將返回已添加到成績簿的測試結果的平均成績。我對編寫這些循環非常感興趣,所以請耐心等待,但是當我將get放入get方法的括號中時,result.get會出現編譯錯誤。我在想我是對的,我明顯不這樣做,所以任何幫助都不勝感激。在此先感謝,對於java中的每個循環

+2

1)在哪裏'foreach'循環? 2)什麼是錯誤? –

+0

您發佈的代碼中沒有任何foreach。 – aliteralmind

+0

'foreach'循環看起來像這樣:'foreach(TestResult result:results){...}'。你只是有一個普通的'for'循環 – Franklin

回答

0

首先,我懷疑你的代碼編譯,因爲你averageGrade()方法不返回任何東西......

試試這個:

public int averageGrade() { 
     int totalGrade=0; 
     int i = 0; 
     for (; i < results.size(); i++); 
     { 
      TestResult theResult = results.get(i); 
      totalGrade += theResult.getGrade(); 
     } 
     if (i>0) 
      return totalGrade/i; 
     else return 0; 

    } 
+0

是的,我忘了添加一個返回this.averageGrade,我剛剛添加。 – user3423682

1

你有

for (int i = 0; i < results.size(); i++); 

     { 
      TestResult theResult = results.get(i); 
      int averageGrade = theResult.getGrade(); 

     } 

你不能在for循環分號,你需要返回一個int ...所以你很可能需要需要

int averageGrade; 
for (int i = 0; i < results.size(); i++) 
     { 
      TestResult theResult = results.get(i); 
      averageGrade = theResult.getGrade(); 

     } 
return averageGrade; 

他們有辦法,Java認爲For循環是自己的。然後它會看到這些隨機大括號,並且對它們爲什麼會出現這個問題感到非常困惑,因此出現編譯時錯誤。它也認爲你會返回一個int,並且你不會再被混淆。