第十一章 异常

第一题  

编写一个类ExceptionTest1,在main方法中使用try、catch、finally:

  1. 在try块中,编写被0除的代码,运行并观察结果。
  2. 在finally块中,打印一条语句。
  3. 增加catch块,捕获被0除所产生的算术运算异常,并且打印异常信息。

 

public class ExceptionTest1 {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("请输入除数:");
        int a = input.nextInt();
        System.out.println("请输入被除数:");
        int b = input.nextInt();
        double c;
        try {
            c = a / b;
            System.out.println(c);
        } catch (ArithmeticException e) {
            System.out.println(e);
        }
        finally {
            System.out.println("biubiubiu.......");
        }
    }


}

第二题 

编写一个方法完成转账功能,转账方法需要提供转出账号、转入账号、要转账的金额。

  1. 当传入转出的帐号(转入帐号和转出帐号)不正确时,转账方法抛出帐户不存在的自定义异常:NotExistsAccountException
  2. 当转出账户的余额不足时,转账方法抛出一个:NotEnoughBlanceException异常,异常包含转入/转出账户的余额不足的相关信息。
  3. 提供测试程序,并对以上的两种异常信息显示出来。

NotEnoughBlanceException.class

public class NotEnoughBlanceException extends Exception {
    public NotEnoughBlanceException(String message) {
        super(message);
    }

}

 NotExistsAccountException.class

public class NotExistsAccountException extends Exception {
    public NotExistsAccountException(String message) {
        super(message);
    }

}

TransferAccounts.class

public class TransferAccounts {
    public static Account[] accounts = {new Account("张三", "1234", 5000.0),
        new Account("李四","2345",10000.0),
        new Account("王五","3456",7000.0)};

    public static void money() throws  NotExistsAccountException,NotEnoughBlanceException{


        Scanner input = new Scanner(System.in);
        System.out.println("请输入转出的账号:");
        String outAccount = input.next();
        for (int i = 0; i < accounts.length; i++) {
            if (accounts[i].getNum().equals(outAccount)) {
                break;
            } else {
                if (i + 1 == accounts.length) {

                    throw new NotExistsAccountException("转出账户不存在!");

                }
            }
        }


        System.out.println("请输入转入的账号:");
        String inAccount = input.next();
        for (int i = 0; i < accounts.length; i++) {
            if (accounts[i].getNum().equals(inAccount)) {
                break;
            }
            else {

                if (i + 1 == accounts.length && !accounts[i].getNum().equals(inAccount)) {
                    throw new NotExistsAccountException("转入账户不存在!");

                }
            }
        }

        System.out.println("请输入要转账的数额:");
        double nextDouble = input.nextDouble();
        for (Account account : accounts) {
            if (account.getNum().equals(outAccount)) {
                if (account.getMoney() < nextDouble) {
                    throw new NotEnoughBlanceException("余额不足!");
                }


                else {
                    account.setMoney(account.getMoney()-nextDouble);
                }
            }
            if (account.getNum().equals(inAccount)) {
                account.setMoney(account.getMoney()+nextDouble);
            }
        }


    }

    public static  void display() {
        for (Account account : accounts) {
            System.out.println("姓名:"+account.getName()+"\t账号:"+account.getNum()+"\t余额:"+account.getMoney());
        }
    }




    public static void main(String[] args) throws NotExistsAccountException, NotEnoughBlanceException {

        TransferAccounts.display();
        TransferAccounts.money();
        TransferAccounts.display();

    }
}


Account.class

public class Account {
    private  String name;
    private  String num;
    private  Double money;

    public Account(String name, String num, Double money) {
        this.name = name;
        this.num = num;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }
}

 

第三题

编写一个方法参数接收一个字符串,返回一个Date对象;该方法能够将多种格式的字符串参数解析成Date.

DateDemo.class

public class DateDemo {
    static String[] patterns = {
            "yyyy年MM月dd日 hh:mm:ss",
            "yyyy-MM-dd HH:mm:ss",
            "yyyy/MM/dd HH:mm:ss",
            "MM/dd/yyyy HH:mm:ss",
            "yyyy-MM-dd",
            "yyyy年MM月dd日",
            "MM/dd/yyyy",
            "yyyyMMdd"};

    public static Date parse(String a) {
        Date date = null;
        for (int i = 0; i < patterns.length; i++) {
            try {

                SimpleDateFormat sdf = new SimpleDateFormat(patterns[i]);
                date = sdf.parse(a);
                return date;


        } catch(ParseException e){
            System.out.println("第"+(i+1)+"次失败\t"+e);
        }


    }

        return date;
    }

    public static void main(String[] args) {
        System.out.println(parse("20221022"));


    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值