栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

7-3 jmu-Java-06异常-04-自定义异常(综合)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

7-3 jmu-Java-06异常-04-自定义异常(综合)

题目:

定义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,加分不成功。
 代码:
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        while (in.hasNext()){
            String s= in.nextLine();
            if (s.equals("others")){
                break;
            }
            else if (s.equals("new")){
                try {
                    Student k=new Student();
                    String y=in.nextLine();
                    String[] x=y.split(" ");
                    if (x.length==2){
                        k.setName(x[0]);
                        int a=k.addScore(Integer.parseInt(x[1]));
                        if (k.getName()!=null&&a!=-1) System.out.println(k);
                    }
                    else System.out.println("java.util.NoSuchElementException");
                }
                catch (Exception e){
                    System.out.println(e);
                }
            }
        }
        System.out.println("scanner closed");
    }
}
class Student {
    private String name;
    private int score;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if ((int)name.charAt(0)<57&&(int)name.charAt(0)>48){
            try {
                throw new IllegalNameException(name);
            }
            catch (IllegalNameException e){
                System.out.println(e);
            }
        }
        else this.name = name;
    }

    public int addScore(int score){
        if (score>100||score<0) {
            try {
                throw new IllegalScoreException(score);
            } catch (IllegalScoreException e) {
                System.out.println(e);
                //e.printStackTrace();
            }
            return -1;
        }
        else this.score=score;
        return this.score;
    }

    @Override
    public String toString() {
        return "Student [" + "name=" + name  + ", score=" + score + ']';
    }
}
class IllegalScoreException extends Exception{
    private int score;
    public IllegalScoreException(int s){
        super("IllegalScoreException: score out of range, score=");
        this.score=s;
    }

    @Override
    public String toString() {
        return this.getMessage()+score;
    }
}
class IllegalNameException extends Exception{
    private String s;
    public IllegalNameException (String name){
        super("IllegalNameException: the first char of name must not be digit, name=");
        this.s=name;
    }

    @Override
    public String toString() {
        return this.getMessage()+this.s;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/435043.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号