import java.util.Scanner;
class A{
int score;
public A(int score) throws Exception{
if(score<0||score>100) {
throw new Exception("Error,score should be in 0-100.");
}
else{
this.score=score;
}
}
void shuchu() {
System.out.println("Your score is:"+score);
}
}
public class Main{
public static void main(String[] args) {
System.out.println("Please input your score");
Scanner in=new Scanner(System.in);
int x=in.nextInt();
try {
A a=new A(x);
a.shuchu();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Java-键盘输入成绩,自定义异常类MyException用于检测输入的成绩大于100或者小于0时,抛出异常,否则就输出成绩
最新推荐文章于 2023-11-13 10:23:13 发布