【优化】自定义抛出throw 对象练习
2024-10-17 01:56:12
package ltb6w;
import java.util.*; public class Bank { private boolean bool=true;
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{//存款操作 try { inputbalance=sc.nextInt();// 容易出异常的地方 }catch (InputMismatchException e) { System.out.println("请输入数字:"); this.bool=false;
} 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 void getBank() { System.out.println("是否继续存款:是or否"); select=sc.next(); // 容易出异常的地方 if("否".equals(select)) { while (true) { System.out.println("看仔细了!!!是否还继续取款:是or否"); try {
select2=sc.next(); // 容易出异常的地方 }catch (InputMismatchException e) { System.out.println("不要输入(是or否)之外无关的数据,从来,再来。"+e.getMessage()); } if("是".equals(select2)) { try {
withdrawa(); break;
} catch (Exception e) { e.printStackTrace();
} }else if ("否".equals(select2)){ System.out.println("不管你选不选否都当做退出。["+select2+"]"); break;
} } bool=false; System.out.println("已经成功退出"); }else if("是".equals(select)) {
System.out.println("继续输入金额,come on baby!");
}
} public static void main(String[] args) throws Exception { Bank b=new Bank(); while (b.bool) { b.deposite();
b.getBank(); } } }
最新文章
- 如何在移动设备上调试html5开发的网页
- Python实现冒泡排序
- lnmp 在nginx中配置相应的错误页面error_page
- Unity Built-in Shader详解二
- Deep Learning Workbench Installation Notes
- bzoj1937
- ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据
- nosql newsql
- js的意义,引用方法及变量
- spring Annotation 笔记2.1
- Intel Core Microarchitecture Pipeline
- Opencv基础课必须掌握:滑动条做调色盘 -OpenCV步步精深
- Django--CRM
- HDU 5832 A water problem 水题
- More is better-多多益善
- 微信小程序 --- 页面跳转
- 数据链路层、ARP/RARP、ICMP、ping和traceroute
- python文件操作各种模式和常用方法总结r r+ rb r+b
- Maven:Non-resolvable parent POM: Failure to find错误
- nyoj 740 “炫舞家“ST
热门文章
- Xposed MultiDex Hook
- Python之路,第五篇:Python入门与基础5
- aliyun服务器对象存储oss
- openssl:AES CBC PKCS5 加解密 (C/GOLANG)
- vue查缺补漏题
- 简易计算器的java实现
- MyBatis #{} 取值注意事项
- HDU 1160:FatMouse's Speed(LIS+记录路径)
- 10 HashMap,Map.Entry,LinkedHashMap,TreeMap,Hashtable,Collections类
- hdu3613 Best Reward manacher+贪心+前缀和