【优化】自定义抛出throw 对象练习
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 对象练习的更多相关文章
- 自定义抛出throw 对象练习
package ltb6w; import java.util.*; public class Bank { private String select; private String select2 ...
- 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 ...
随机推荐
- php 怎样将有范围的ip转化为整型范围
php中将IP转换成整型的函数ip2long()容易出现问题,在IP比较大的情况下,会变成负数.如下: <?php $ip = "192.168.1.2"; $ip_n = ...
- Unescaped left brace in regex is illegal here in regex; marked by <-- HERE in m/\${ <-- HERE ([^ \t=:+{}]+)}/ at xxxx/usr/bin/automake line 3939.
/********************************************************************** * Unescaped left brace in re ...
- SEGMENTATION FAULT IN LINUX 原因与避免
https://www.cnblogs.com/no7dw/archive/2013/02/20/2918372.html
- 基于hiredis,redis C客户端封装
项目中需要用到redis就封装了一下,基于hiredis,只封装了string和哈希的部分方法.编译时加入-D__USER_LOCK__添加线程安全. suntelRedisCli.h #ifndef ...
- Java设计模式(二)
3.设计模式分类 通常来说设计模式分为三大类,共23种: 1.工厂模式 工厂模式(Factory Pattern)的意义就跟它的名字一样,在面向对象程序设计中,工厂通常是一个用来创建其他对象的对象 ...
- NET Core 实战 Dapper 扩展数据访问
NET Core 实战:基于 Dapper 扩展你的数据访问方法 一.前言 在非静态页面的项目开发中,必定会涉及到对于数据库的访问,最开始呢,我们使用 Ado.Net,通过编写 SQL 帮助类帮我们实 ...
- 《DSP using MATLAB》Problem 6.13
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- Liunx----vi编辑器
关于vi编辑器基本的用法总结如下: vi是linux下的一款文编编辑器. 进入vi的方法:在系统启动起来后,直接键入vi或vi 文件名: 进去后输入内容方法:输入a或i,切换到编辑模式: 退出编辑模式 ...
- python------模块定义、导入、优化 ------->Shelve模块
1. shelve模块 是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式. import shelve import datetime d = ...
- 【shell编程】之基础知识-函数
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...