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

自定义抛出throw 对象练习的更多相关文章

  1. 【优化】自定义抛出throw 对象练习

    package ltb6w; import java.util.*; public class Bank { private boolean bool=true; private String sel ...

  2. java中异常的抛出:throw throws

    java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...

  3. java中抛出throw关键字是怎么用的? 举例?

    5.抛出throw关键字 马克-to-win:我们先说5/0的原理,当程序运行到5/0的时候,java系统JVM会在后台new出一个除0异常实例,之后把这个实例传入catch块儿供开发者使用.马克-t ...

  4. java 異常抛出 throw 與 return

    package 異常;    public class TestException {      public TestException() {      }        boolean test ...

  5. java-异常-原理异常对象的抛出throw

    1 class Demo { 2 public static int method(int[] arr,int index) { 3 4 // System.out.println(arr[index ...

  6. java自定义抛出的异常Exception

    package com.zhanzhuang.exception; public class CustomizeException { public static void main(String[] ...

  7. 133.throw机制 抛出类类型

    #include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...

  8. Java异常处理-----抛出处理

    抛出处理 定义一个功能,进行除法运算例如(div(int x,int y))如果除数为0,进行处理. 功能内部不想处理,或者处理不了.就抛出使用throw new Exception("除数 ...

  9. 微信小程序-自定义方法的抛出与引用

    一. 定义方法与抛出(utils/foo.js文件中) function say () { console.log('自定义的say方法')}  # 定义方法 module.exports = {sa ...

随机推荐

  1. JavaBasic_02

    Java的基础框架 3W:What How Why What:一个东西是什么,具备什么样的功能 怎么用 How: 怎么做?功能如何实现 读源代码(jdk)->学习很多,优雅的编程技巧建立在wha ...

  2. 百练8216-分段函数-2016正式A题

    百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问   A:分段函数 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 编写程序 ...

  3. vs2015连接mysql进行数据库操作

    要求:电脑提前安装好vs,mysql. 1.在需要连接mysql的项目上右键选择“属性” -> “C/C++” -> “常规” ->选择“附加包含目录” 在弹出窗口中添加mysql的 ...

  4. animate.css动画

    添加类名的时间不要只添加动画的类名,也要加上animated,使用的时间可以把自己需要的效果复制出来

  5. opencv感兴趣区域ROI

    addWeighted //显示原图 Mat src = imread("data/img/1.jpg"); imshow("src",src); //显示lo ...

  6. linux下使用小票打印

    linux下使用小票打印 打印机: Xprinter XP-58IIH指令支持: ESC/POS接口: USB, 蓝牙 Linux系统: Centos7 蓝牙配对很快, 配对好后就是连接状态. 但很快 ...

  7. python在图片上画矩形

    python在图片上画矩形 image_path = '' image = cv2.imread(image_path) first_point = (100, 100) last_point = ( ...

  8. poj 2155 B - Matrix 二维树状数组

    #include<iostream> #include<string> #include<string.h> #include<cstdio> usin ...

  9. Ubuntu下一个好用的终端

    在终端下输入: sudo apt-get install terminator 快捷键: shift+ctrl+e          在当前窗口右侧新开一个窗口 shift+ctrl+w        ...

  10. 《DSP using MATLAB》Problem 5.3

    这段时间爬山去了,山中林密荆棘多,沟谷纵横,体力增强不少. 代码: %% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...