题目:
【问题描述】
键盘输入成绩,自定义异常类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
代码:
import java.util.Scanner;
class MyException extends Exception{
String message;
public MyException(int score){
message = "Error,score should be in 0-100.";
}
}
class My{
public My(int s)throws MyException{
if(s>100||s<0)
{
throw new MyException(s);
}
else {
System.out.println("Your score is:"+s);
}
}
}
public class zhong{
public static void main(String[] args) throws MyException {
Scanner in=new Scanner(System.in);
System.out.println("Please input your score");
int score=in.nextInt();
try {
new My(score);
}
catch(MyException e) {
System.out.println(e.message);
}
}
}