2012-04-27 43 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

相关问题