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!");
} } } }

自定义抛出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. 【转载】 996是没前途的!996.ICU来了,回忆我对996的态度是如何从支持变成了怀疑!

    原文地址: https://www.meiqiantu.com/20138.html --------------------------------------------------------- ...

  2. PHP安全之Web攻击(转)

    一.SQL注入攻击(SQL Injection) 攻击者把SQL命令插入到Web表单的输入域或页面请求的字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响)动态 ...

  3. pandas apply()函数参数 args

    #!/usr/bin/python import pandas as pd data = {'year':[2000,2001,2002,2001,2002],'value':[1.5,1.7,3.6 ...

  4. [Algorithm] Good Fibonacci

    def good_fibonacci(n): if n<=1: return (n,0) else: (a,b)=good_fibonacci(n-1) return (a+b,a)

  5. leetcode 772.Basic Calculator III

    这道题就可以结合Basic Calculator中的两种做法了,分别是括号运算和四则运算的,则使用stack作为保持的结果,而使用递归来处理括号内的值的. class Solution { publi ...

  6. python学习1 ---range()函数

    奇怪的现象 在paython3中 print(range(10)) 得出的结果是 range(0,10) ,而不是[0,1,2,3,4,5,6,7,8,9] ,为什么呢? 官网原话: In many ...

  7. HDU2023:求平均成绩

    Problem Description 假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量. ...

  8. 四则运算V1.1

    作业:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/997 代码:https://coding.net/u/Dawnfox/p/f4 ...

  9. ThinkPHP验证码类的使用

    1.创建一个方法并引入验证码类class ShowAction extends Action{//用户评论验证码public function verify(){import('ORG.Util.Im ...

  10. ie6,ie7下a标签无法点击(转载)

    前几天在做一个网站的引导页面,因为都是用图片,所以按钮需要用空a标签来做,发现a标签在IE6与IE7中点击无效中点击不了,其他浏览器都正常.一开始以为是z-index的问题,但不论z-index设置多 ...