1 package ltb6w; 2 import java.util.*; 3 4 public class Bank { 5 6 7 private boolean bool=true; 8 private String select; 9 private String select2; 10 private double balance=0.0; //余额 11 private double inputbalance=0.0; 12 13 private Scanner sc=new Scanner(System.in); 14 15 16 public Bank() { 17 18 System.out.println("请输入存款额度:"); 19 20 21 } 22 23 24 25 @SuppressWarnings("serial") 26 class NotSufficientFunds extends Exception { //自定义异常类 27 28 29 private String insufficient; 30 31 NotSufficientFunds(String s ) { 32 33 insufficient=s; 34 } 35 36 public String getMessage() { 37 38 return insufficient; 39 40 } 41 42 } 43 44 45 public void deposite() throws Exception{//存款操作 46 47 48 try { 49 50 inputbalance=sc.nextInt();// 容易出异常的地方 51 52 }catch (InputMismatchException e) { 53 54 System.out.println("请输入数字:"); 55 56 this.bool=false; 57 } 58 59 60 if(inputbalance<0) { 61 62 throw new NotSufficientFunds("存款不能是负数"); //抛出自定义异常 63 } 64 65 66 67 this.balance=inputbalance+balance; 68 69 System.out.println("存款总额:"+balance); 70 } 71 72 public void withdrawa() throws Exception{//取款操作 73 74 75 System.out.println("请输入取款数:"); 76 77 78 this.balance=balance-sc.nextInt(); 79 80 if (balance<0) { //激发异常类 81 82 throw new NotSufficientFunds("余额已经是负数了"); 83 84 } 85 86 87 System.out.println("余额:"+this.getbalawal()); 88 } 89 90 public double getbalawal() { //获取余额操作 91 92 return balance; 93 } 94 95 96 public void getBank() { 97 98 System.out.println("是否继续存款:是or否"); 99 100 select=sc.next(); // 容易出异常的地方 101 102 103 if("否".equals(select)) { 104 105 106 while (true) { 107 108 System.out.println("看仔细了!!!是否还继续取款:是or否"); 109 110 try { 111 select2=sc.next(); // 容易出异常的地方 112 113 }catch (InputMismatchException e) { 114 115 System.out.println("不要输入(是or否)之外无关的数据,从来,再来。"+e.getMessage()); 116 117 118 } 119 120 if("是".equals(select2)) { 121 122 try { 123 withdrawa(); 124 125 break; 126 } catch (Exception e) { 127 128 e.printStackTrace(); 129 } 130 131 }else if ("否".equals(select2)){ 132 133 System.out.println("不管你选不选否都当做退出。["+select2+"]"); 134 135 break; 136 } 137 138 } 139 140 bool=false; 141 142 System.out.println("已经成功退出"); 143 144 145 }else if("是".equals(select)) { 146 System.out.println("继续输入金额,come on baby!"); 147 } 148 } 149 150 151 public static void main(String[] args) throws Exception { 152 153 154 Bank b=new Bank(); 155 156 while (b.bool) { 157 158 159 b.deposite(); 160 b.getBank(); 161 162 } 163 164 } 165 166 } 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190