自定义抛出throw 对象练习
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 对象练习的更多相关文章
- 【优化】自定义抛出throw 对象练习
package ltb6w; import java.util.*; public class Bank { private boolean bool=true; private String sel ...
- java中异常的抛出:throw throws
java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...
- java中抛出throw关键字是怎么用的? 举例?
5.抛出throw关键字 马克-to-win:我们先说5/0的原理,当程序运行到5/0的时候,java系统JVM会在后台new出一个除0异常实例,之后把这个实例传入catch块儿供开发者使用.马克-t ...
- java 異常抛出 throw 與 return
package 異常; public class TestException { public TestException() { } boolean test ...
- java-异常-原理异常对象的抛出throw
1 class Demo { 2 public static int method(int[] arr,int index) { 3 4 // System.out.println(arr[index ...
- java自定义抛出的异常Exception
package com.zhanzhuang.exception; public class CustomizeException { public static void main(String[] ...
- 133.throw机制 抛出类类型
#include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...
- Java异常处理-----抛出处理
抛出处理 定义一个功能,进行除法运算例如(div(int x,int y))如果除数为0,进行处理. 功能内部不想处理,或者处理不了.就抛出使用throw new Exception("除数 ...
- 微信小程序-自定义方法的抛出与引用
一. 定义方法与抛出(utils/foo.js文件中) function say () { console.log('自定义的say方法')} # 定义方法 module.exports = {sa ...
随机推荐
- [LeetCode&Python] Problem 598. Range Addition II
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- Gym-101653:acific Northwest Regional Contest (2019训练第一场)
本套题没有什么数据结构题,图论题,唯一有价值的就是Q题博弈,在最后面,读者可以直接拉到最下面. (还剩下两个,估计每什么价值的题,懒得补了 M .Polyhedra pro:欧拉公式,V-E+F=2: ...
- 网络IP和网络掩码以及网关的学习
192.168.10.1/24,192.168.1.1/30是什么意思啊? 表示地址范围你把这些前面的值转换成2进制 就是变成一个32位的地址比如192.168.10.1就变成11000000.101 ...
- debian 配置linuxptp 软件时间戳
编程之路刚刚开始,错误难免,希望大家能够指出. ntp,ptp,ntp,ptp 本文只说软件时间戳 先上几个推荐的网址,可以更好的了解ptp: https://docs.fedoraproject.o ...
- LeetCode - Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- 【BZOJ1213】高精度开根
python是坠吼的! 原题: 不贴原题,就是高精度开根,结果向下取整 首先二分答案,高精度嘛……python即可 二分右端点设为n会T掉,需要先倍增一个r,while(r **m <= n) ...
- test20190331
贪吃蛇(snaker.cpp/.in.out 1S 512M) [题目描述] C 最近爱上了玩贪吃蛇,想要自己制作一款贪吃蛇游戏,但是她太弱了,只会写一个一维的程序. 游戏界面可以看做一条长为 n 的 ...
- webpack 搭建问题汇总
总结一下遇到的问题: 1.这样的警告(The 'mode' option has not been set, webpack will fallback to 'production' for thi ...
- linux 之sed
sed 用法 sed [-nefr] [action] -i 直接修改文件内容,而不是像其他命令那样只是输出到终端 a新增c取代d删除i插入p列印常与sed -n 使用s取代 有一点需要注意的是:如果 ...
- Java打印九九乘法表及倒打九九乘法表
//正打 public class Test3 { public static void main(String[] args) { for(int j=1;j<10;j++){ for(int ...