1、
【问题描述】
键盘输入成绩,自定义异常类MyException用于检测输入的成绩大于100或者小于0时,抛出异常,否则就输出成绩。
【输入形式】
请输入你的成绩:
【输出形式】
如果成绩在【0-100】:你的成绩为:
如果成绩不在【0-100】:错误,成绩应该在【0-100】之间
【输入输出样例1】
Please input your score
101
Error,score should be in 0-100.
【输入输出样例2】
Please input your score
90
Your score is:90
package learn;
public class MyException extends Exception{
String message;
public MyException(){
message="Error,score should be in 0-100.";
}
public String warnMess(){
return message;
}
}
package learn;
public class Score {
int score;
public Score(int score) throws MyException{
if(score<0 || score>100){
throw new MyException();
}else {
this.score=score;
System.out.println("Your score is:"+score);
}
}
}
package learn;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println(