定义IllegalScoreException
异常类,代表分数相加后超出合理范围的异常。该异常是checked exception
,即希望该异常一定要被捕获处理。
定义IllegalNameException
异常类,代表名字设置不合理的异常。该异常是unchecked exception
定义Student
类。
属性:
private String name;
private int score;
方法:
toString //自动生成
setter/getter //自动生成
改造setName //如果姓名首字母为数字则抛出IllegalNameException
public int addScore(int score) //如果加分后分数<0 或>100,则抛出IllegalScoreException,加分不成功。
main方法:
- 输入
new
则新建学生对象。然后输入一行学生数据,格式为姓名 年龄
,接着调用setName,addScore。否则跳出循环。 - setName不成功则抛出异常,并打印异常信息,然后继续下一行的处理。
- addScore不成功则抛出异常,并打印异常信息,然后继续下一行的处理。如果2、3都成功,则打印学生信息(
toString
) - 如果在解析学生数据行的时候发生其他异常,则打印异常信息,然后继续下一行的处理。
- Scanner也是一种资源,希望程序中不管有没有抛出异常,都要关闭。关闭后,使用
System.out.println("scanner closed")
打印关闭信息
注意:使用System.out.println(e);
打印异常信息,e为所产生的异常。
输入样例:
new
zhang 10
new
wang 101
new
wang30
new
3a 100
new
wang 50
other
输出样例:
Student [name=zhang, score=10]
IllegalScoreException: score out of range, score=101
java.util.NoSuchElementException
IllegalNameException: the first char of name must not be digit, name=3a
Student [name=wang, score=50]
scanner closed
作者: 郑如滨
单位: 集美大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
import java.util.Scanner;
class IllegalScoreException extends Exception{
public IllegalScoreException() {
}
public IllegalScoreException(String message) {
super(message);
}
public IllegalScoreException(String message, Throwable cause) {
super(message, cause);
}
public IllegalScoreException(Throwable cause) {
super(cause);
}
}
class IllegalNameException extends Exception{
public IllegalNameException() {
}
public IllegalNameException(String message) {
super(message);
}
public IllegalNameException(String message, Throwable cause) {
super(message, cause);
}
public IllegalNameException(Throwable cause) {
super(cause);
}
}
class Student{
private String name;
private int score;
//一般情况下,需要注意若程序抛出一个异常后,程序停止执行
private int flag=0;
public String getName() {
return name;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public void setName(String name) {
char c=name.charAt(0);
if (c>='0'&&c<='9'){
try {
flag=1;
throw new IllegalNameException();
} catch (IllegalNameException e) {
System.out.println("IllegalNameException: the first char of name must not be digit, name="+name);
}
return;
}
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
if (flag==1){
return;
}
if (score<0||score>100){
try {
flag=1;
throw new IllegalScoreException();
} catch (IllegalScoreException e) {
System.out.println("IllegalScoreException: score out of range, score="+score);
}
}
this.score = score;
}
//如果加分后分数<0 或>100,则抛出IllegalScoreException,加分不成功。
public int addScore(int score) {
return 0;
}
@Override
public String toString() {
return "Student [" +
"name=" + name +
", score=" + score +
']';
}
}
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while (true){
String str=sc.nextLine();
if (str.equals("new")){
String s=sc.nextLine();
String[] s1 = s.split(" ");
if (s1.length==2){
String name=s1[0];
int score=Integer.valueOf(s1[1]);
Student student = new Student();
student.setName(name);
student.setScore(score);
if (student.getFlag()==0){
System.out.println(student);
}
}else{
System.out.println("java.util.NoSuchElementException");
}
}else {
break;
}
}
sc.close();
System.out.println("scanner closed");
}
}