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(); } } }

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

  1. 自定义抛出throw 对象练习

    package ltb6w; import java.util.*; public class Bank { private String select; private String select2 ...

  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. PHP目前比较常见的五大运行模式SAPI(转)

    运行模式 关于PHP目前比较常见的五大运行模式: 1)CGI(通用网关接口/ Common Gateway Interface) 2)FastCGI(常驻型CGI / Long-Live CGI) 3 ...

  2. Python之路,第五篇:Python入门与基础5

    python 循环语句 作用:  根据一定的条件,重复的执行一个或多个语句 两种循环语句: while 语句 for 语句 while 语句: 语法: while    真值表达式: 语句1 ... ...

  3. [LeetCode&Python] Problem 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. 单调栈的运用-bzoj1012(代码转载-http://hzwer.com/1130.html)

    Description 现在请求你维护一个数列,要求提供以下两种操作: . 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. . 插 ...

  5. 关于 transparent rgba display:none; opacity visiblity 关于em

    关于 transparent  rgba   display:none; opacity   visiblity   display 之后不会占位. 其余都会占位 opacity 还会继承,子元素也会 ...

  6. Bow and Arrow Rigging in Blender

    https://www.youtube.com/watch?v=jpsd0Aw1qvA 新建骨架,由如下图3部分组成: Bone.000.Top ~ Bone.015.Top (上半部分16节骨骼) ...

  7. hdu4990 Reading comprehension 矩阵快速幂

    Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:10240 ...

  8. day18-19 Storm

    课程介绍 课程名称:Storm是什么 课程目标: 通过该课程的学习能够了解离线计算与流式计算的区别.掌握Storm框架的基础知识.了解流式计算的一般架构图. 课程大纲: 1. 离线计算是什么? 2.  ...

  9. 在Android上运行Java和C程序

    在linux上运行java程序,直接用javac编译,再用java启动虚拟机运行就行了.但是在Android上,由于虚拟机和pc端的不同,所以操作方法也是不一样的.下面介绍Android上运行Hell ...

  10. Gravitee.io docker-compose运行

    Gravitee.io 是一个相对比较完整的api gateway 平台,包含了api 相对比较完整的生命周期管理 同时在访问控制以及日志监控上也做的比较好,是一款可以尝试试用的api gateway ...