题解 | #判断学生成绩#
判断学生成绩
http://www.nowcoder.com/practice/a35cbafbec10449f8a576e822430a3ab
import java.util.*;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt();
//write your code here......
scanner.close();//**关闭输入器
try{
if(0<=score&&score<=100){
System.out.println(score);
}
else{
throw new ScoreException("分数不合法");//throw抛出异常,并且创建一个新的异常对象。ScoreException("分数不合法")是我们自定义的异常构造器。
}
//try和catch是主函数里抓取潜在异常的一对组合,无异常的话catch就不会执行,有catch就会抓取thorw新建的异常对象,然后输出我们需要的提示信息。
}
catch(ScoreException e){
System.out.println(e.getMessage());
}//e是ScoreException类,继承了父类的getMessage的方法。
}
}
class ScoreException extends Exception {
//write your code here......
public ScoreException(String Message){
super(Message);
}
}