题解 | #判断学生成绩#
判断学生成绩
http://www.nowcoder.com/practice/a35cbafbec10449f8a576e822430a3ab
import java.util.*;
public class Main {
public static void main(String[] args) {
// // 定义一个方法用于录入学生的考试成绩,要求考试成绩必须在0-100之间, // 不满足就产生一个自定义异常,控制台输出一个错误信息"分数不合法" // (请输出自定义异常对象的错误信息,将错误信息设置为分数不合法)
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
//write your code here......
try {
ScoreException.customizeException( score );
}catch ( RuntimeException e ){
System.out.println( e.getMessage().toString() );
}
}
}
class ScoreException extends Exception {
//write your code here......
public static void customizeException(int score){
if ( score <= 100 && score >= 0 ){
System.out.println(score);
}else {
throw new RuntimeException("分数不合法");
}
}
}