简体   繁体   中英

How do i modify my IllegalArgumentException to include negative values?

The question statement:

Write a method called getGrade that accepts an integer representing a student's grade in a course and returns that student's numerical course grade. The grade can be between 0.0 (failing) and 4.0 (perfect). Assume that scores are in the range of 0 to 100 and that grades are based on the following scale: and make the method throw an IllegalArgumentException if the user passes a grade lower than 0 or higher than 100.

It works for everything but negative values... so if the user input -1 its giving me 0.0 rather than an error.

Here is my code:

public double getGrade(int score) {
   double grade = 0;
   if(score<60) {
     grade = 0.0;
     return grade;
   } else if (score>=60 && score<=62) {
         grade = 0.7;
      return grade;
   } else if (score>=63 && score<95) {
         grade = (0.7+(0.1*(score-62)));
      return grade;
   } else if (score>=95 && score<100) {
         grade = 4.0;
      return grade;
   } else {
      throw new IllegalArgumentException();
  }
}

请尝试将if(score<60) {修改为if(score>=0 && score<60) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM