如何修改我的 IllegalArgumentException 以包含负值?
How do i modify my IllegalArgumentException to include negative values?
问题陈述:
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.
它适用于除负值以外的所有值...
所以如果用户输入 -1 它给我 0.0 而不是错误。
这是我的代码:
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) {
。
问题陈述:
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.
它适用于除负值以外的所有值... 所以如果用户输入 -1 它给我 0.0 而不是错误。
这是我的代码:
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) {
。