2020-11-17

java异常类基础复习

在这里插入代码片
package ssudt.com.cok;//设计一个项目,客户可以从银行里取钱,但是每次只能取500,超过就会报错

import java.util.Scanner;

class overnumberException extends Exception{
    String message;
    overnumberException(){
        message="存取的钱超过了100,取太多了,给爷爬";
    }
    public String toString(){
        return message;
    }
}
class bank{
    double deposite;
    public void savemoney(double amount){
        deposite=deposite+amount;
        System.out.println("存钱可没有限制");
    }
    bank(double deposite){
        this.deposite=deposite;
    }
    public void countmoney(double amount ) throws overnumberException{
        if(amount>100)
            throw new overnumberException();
        else
            deposite=deposite-amount;
        System.out.println("算你识相");
    }
    public double getRestMoney(){
        return deposite;
    }

}
public class Main {
    public static void main(String[]args){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入账户内本来有的本金");
        bank Bank=new bank(input.nextDouble());
        try {
            System.out.println("请输入要存入的金额");
            Bank.savemoney(input.nextDouble());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("显示剩余的费用");
            System.out.println(Bank.getRestMoney());
            System.out.println("请输入要取走的金额");
            Bank.countmoney(input.nextDouble());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("显示剩余的费用");
            System.out.println(Bank.getRestMoney());
            System.out.println("请输入要取走的金额");
            Bank.countmoney(input.nextDouble());
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("显示剩余的费用");
            System.out.println(Bank.getRestMoney());


        }catch (overnumberException e){
            System.out.println(e.toString());
        }finally {
            System.out.println("利用finally显示最终费用");
            System.out.println(Bank.getRestMoney());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值