2012-04-27 114 views
5

我正在嘗試處理Java任務。這是它要求的:這是使用IllegalArgumentException的正確方法嗎?

寫一個類TestScores。類構造函數應該接受一組測試分數作爲它的參數。該類應該有一個返回測試分數平均值的方法。如果陣列中的測試分數爲負數或大於100,則該班級應拋出IllegalArgumentException。演示。我需要一個名爲TestScoresTestScoresDemo的文件。

這是我到目前爲止。我知道有一些是錯誤的,我需要幫助修復它:

class TestScores { 
    public static void checkscore(int s) { 
     if (s<0) throw new IllegalArgumentException("Error: score is negative."); 
     else if (s>100) throw new IllegalArgumentException("Error Score is higher then 100"); 
     else if (s>89)throw new IllegalArgumentException("Your grade is an A"); 
     else if (s>79 && s<90)throw new IllegalArgumentException("Your grade is an B"); 
     else if (s>69 && s<80)throw new IllegalArgumentException("Your grade is an C"); 
     else if (s>59 && s<70)throw new IllegalArgumentException("Your grade is an D"); 
     else if (s<60)throw new IllegalArgumentException("Your grade is an F"); 

     { 
      int sum = 0; //all elements together 
      for (int i = 0; i < a.length; i++) 
       sum += a[i]; 
     } 
     return sum/a.length; 
    } 
} 

class TestScoresDemo { 
    public static void main(String[] args) { 
     int score = 0; 
     Scanner scanner = new Scanner(System.in); 
     System.out.print(" Enter a Grade number: "); 
     String input = scanner.nextLine(); 
     score = Integer.parseInt(input); 
     TestScores.checkscore(score); 
     System.out.print("Test score average is" + sum); 
    } 
} 

我知道一個try語句分配來電,因爲在我的書,這就是我與IllegalArgumentException看到。誰能幫我?我使用Eclipse作爲IDE。

+2

你爲什麼認爲這是錯的?你有錯誤信息,你不應該?如果我們不知道哪部分需要修復,我們無法提供幫助。 – 2012-04-27 03:00:57

+0

謝謝。讓我仔細檢查並回復你。也許它只是一個簡單的類型錯誤 – Alexandria 2012-04-27 03:04:47

+7

你應該只在拋出一個'IllegalArgumentException'的情況下,在你的情況下,參數無效0或大於100. – twain249 2012-04-27 03:05:03

回答

3

您的TestScores類應該有兩個成員:一個構造函數,接受一組分數和一個返回分數平均值的方法。如果測試分數超出範圍,那麼這些任務並不完全清楚,但是我會將其作爲構造函數(因爲這就是參數)。

public class TestScores { 
    public TestScores(int[] scores) throws IllegalArgumentException { 
     // test the scores for validity and throw an exception if appropriate 
     // otherwise stash the scores in a field for later use 
    } 

    public float getAverageScore() { 
     // compute the average score and return it 
    } 
} 

您的TestScoresDemo課程正處於正確的軌道上。它首先需要將一組分數收集到一個數組中。那麼它應該構造一個TestScores對象。這是try/catch塊中需要的內容,因爲它可能會引發異常。那麼你只需要撥打getAverageScore()並做一些結果。

+0

謝謝大家。我會盡力修改它。出於某種原因,我真的失去了這項任務。 – Alexandria 2012-04-27 03:22:55

+0

@Alexandria - 你試圖在你的'checkscore'方法中做太多。它應該檢查分數並在適當的情況下拋出'IllegalArgumentException';它不應該做任何事情。寫其他方法(和構造函數)來完成問題的其他部分。這應該有助於你回到正軌。 – 2012-04-27 03:34:21

-2
public class TestScores { 
private final int[] scores; 

public TestScores(int[] scores) { 
    this.scores = scores; 
} 

public int getAverage() { 
    int sum = 0; 

    if(scores.length == 0) { 
     return 0; 
    } 

    for(int score: scores) { 
     if(score < 0 || score > 100) { 
      throw new IllegalArgumentException("Score is not valid!"); 
     } 
     sum += score; 
    } 
    return sum/scores.length; 
} 

}

+5

...並且一旦OP複製粘貼這個答案並提交它,那麼分配的整個點將是沒有意義的,並且OP不會從中學到任何東西。 – 2012-04-27 03:14:06

+0

哇。當一個問題被標記爲「家庭作業」時,填答答案的形式很差。閱讀關於如何提問和回答作業問題的[常見問題解答](http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions)。 – 2012-04-27 03:15:02

+1

謝謝。他的職位實際上幫助了其他人的意見和幫助。我可以看到我的差異。我的書裏已經有類似的東西了。我現在只需要添加一個catch並嘗試找出正確的方法。感謝大家。 – Alexandria 2012-04-27 03:41:41

0

一個例外是用來定義一些不正確的去對應用程序的正常流動的東西。當調用checkScore方法時,必須拋出IllegalArgumentException,並且它找到範圍之外的任何參數(0到100之間)。

你的類應該具有這樣的結構:

public class TestScore { 

    private int scores[]; //With setters and getters. 

    public TestScore(int scores[]) { 
     //Here, you set the scores array to the one on this class. 
    } 

    public int getAverage() { 
     //You do the average here, and since you need to iterate over the 
     //array to sum each value, you can check the value and if it's not 
     //ok you throw the IllegalArgumentException. No throws keyword 
     //required since this kind of exception (like NullPointerException 
     //and many others) are unchecked exceptions, meaning they can be 
     //thrown by a method and it does not need to specify them. 
    } 

} 

測試類應該創建一個int數組作爲其構造器的參數的TestScore對象。然後,您創建一個testAverageScore方法,其中包含try-catch語句,因爲它需要調用getAverage方法。

希望有所幫助。祝你好運!。

編輯: IllegalArgumentException是一個未經檢查的異常。

+0

沒有必要爲'scores'設置一個setter;它在構造函數中傳遞。 (它甚至可能是'final')。 – 2012-04-27 03:32:12

+0

這只是開始鼓勵使用標準。如果將來他想爲同一個對象設置一個新陣列呢? – Gamb 2012-04-27 03:33:54

+0

從[Java教程](http://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html):「一個對象被認爲是_immutable_,如果它的狀態在它被構造後不能改變。不可變對象被廣泛接受爲創建簡單可靠代碼的合理策略。「 – 2012-04-27 03:37:08

相關問題