zoukankan      html  css  js  c++  java
  • 自定义抛出throw 对象练习

    package ltb6w;
    import java.util.*;
    
    public class Bank {
        
        private String select;
        private String select2;
        
        private double balance=0.0; //余额
        private double inputbalance=0.0;
        
        private Scanner sc=new Scanner(System.in);
        
    
        public Bank() {
            
            System.out.println("请输入存款额度:");
            
            
        }
        
        
        
        @SuppressWarnings("serial")
        class  NotSufficientFunds extends Exception { //自定义异常类
            
                
            private String insufficient;
    
            NotSufficientFunds(String s ) {
                
                insufficient=s;
            }
            
            public String getMessage() {
                
                return insufficient;
                
            }
            
        }                                              
        
        public void deposite() throws Exception{//存款操作
            
            inputbalance=sc.nextInt();
            
            if(inputbalance<0) {
                
                throw new NotSufficientFunds("存款不能是负数");  //抛出自定义异常
            }
                
            
            
            this.balance=inputbalance+balance;
            
            System.out.println("存款总额:"+balance);
        }        
        
        public void withdrawa() throws Exception{//取款操作
            
            
            System.out.println("请输入取款数:");
            
            
            this.balance=balance-sc.nextInt();
            
        
            if (balance<0) { //激发异常类
        
                    throw new NotSufficientFunds("余额已经是负数了"); 
                
            }    
            
            
            System.out.println("余额:"+this.getbalawal());
        }            
        
        public double getbalawal() { //获取余额操作
            
            return balance;
        }
     
        public static void main(String[] args) {
            
            
            Bank b=new Bank();
            
            while (true) {  
                
                try {
                    b.deposite();
                } catch (Exception e) {
                    
                    e.printStackTrace();
                }
                
                System.out.println("是否继续存款:是or否");
                
                
                try {
                    b.select=b.sc.next(); // 容易出异常的地方
                    
                }catch (InputMismatchException e) {
                    
                    System.out.println("不要输入(是or否)之外无关的存款数据,从来,再来。");
                    
                    continue;
                    
                }
                
                
                
              
                if("否".equals(b.select)) {    
                    
                    
                while (true) {
                    
                    System.out.println("看仔细了!!!是否还继续取款:是or否");
                    
                    try {
                        b.select2=b.sc.next(); // 容易出异常的地方
                        
                    }catch (InputMismatchException e) {
                        
                        System.out.println("不要输入(是or否)之外无关的数据,从来,再来。");
                        
                        continue;
                        
                    }
                    
                    if("是".equals(b.select2)) {
    
                        try {
                            b.withdrawa();
                            
                            break;
                        } catch (Exception e) {
                             
                            e.printStackTrace();
                        }
                        
                    }else if ("否".equals(b.select2)){
                        
                        System.out.println("不管你选不选否都当做退出。["+b.select2+"]");
                        
                        break;
                    }
                    
                }    
                    
                
                System.out.println("已经成功退出");
                
                  break;
            
                }else if("是".equals(b.select)) {
                    System.out.println("继续输入金额,come on baby!");
                }
                
            }
        
    
        }
    
    }
    
  • 相关阅读:
    【python cookbook】【数据结构与算法】4.找到最大或最小的N个元素
    【python cookbook】【数据结构与算法】3.保存最后N个元素
    oracle sql语言模糊查询--通配符like的使用教程
    oracle decode处理被除数为0 的情况
    握草
    oracle if else 判断
    Procdure for wanfo business report
    [转]Oracle 阳历转农历
    阴阳历互相转换方法
    [转]解决crystal report水晶报表在浏览器提示bobj未定义的错误
  • 原文地址:https://www.cnblogs.com/ltb6w/p/8016940.html
Copyright © 2011-2022 走看看