JAVA實現(xiàn)賬戶取款和存款操作
這篇文章主要介紹了JAVA實現(xiàn)賬戶取款和存款操作,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
JAVA 編寫一個程序完成從某賬戶取款和存款的操作
(1)輸入存款金額是,如果非數(shù)值型,捕獲一場并進行處理
(2)操作賬戶類是,如果取款金額大于余額時則做異常處理
import java.util.Scanner;
public class Blank {
public static void main(String[] args) {
float residue = 10000.0f;
String type;
Scanner scn = new Scanner(System.in);
while(true) {
System.out.println("****當(dāng)前賬戶余額:"+residue+"****");
System.out.println("1.存錢 2.取錢 0.退出");
System.out.print("請選擇(1,2,0):");
type = scn.nextLine();
if(type.equals("1")) {
System.out.print("請輸入金額:");
try {
int money = scn.nextInt();
residue = money+residue;
} catch (Exception e) {
// TODO: handle exception
System.out.println("輸入錯誤...");
scn.nextLine();
}
}
else if(type.equals("2")) {
try {
System.out.print("請輸入:");
int money = scn.nextInt();
if(money>residue) {
throw new Exception();
} else {
residue = residue-money;
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("余額不足");
scn.nextLine();
}
}
else if(type.equals("0")) {
System.out.println("bye..");
break;
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Freemaker Replace函數(shù)的正則表達式運用
這篇文章主要介紹了Freemaker Replace函數(shù)的正則表達式運用 的相關(guān)資料,需要的朋友可以參考下2015-12-12
Spring不能注入Static變量的原因及Spring注入靜態(tài)變量
這篇文章主要介紹了Spring不能注入Static變量的原因及Spring注入靜態(tài)變量,需要的朋友可以參考下2016-01-01
Java文件讀寫IO/NIO及性能比較詳細代碼及總結(jié)
這篇文章主要介紹了Java文件讀寫IO/NIO及性能比較詳細代碼及總結(jié),具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
JAVA中關(guān)于Long類型返回前端精度丟失問題處理辦法
這篇文章主要介紹了后端JavaBean的id屬性從Long類型改為雪花算法后出現(xiàn)的精度丟失問題,解決方案包括將id字段類型改為字符串或使用Jackson序列化方式,需要的朋友可以參考下2024-11-11
使用Spring源碼報錯java:找不到類 InstrumentationSavingAgent的問題
這篇文章主要介紹了使用Spring源碼報錯java:找不到類 InstrumentationSavingAgent的問題,本文給大家分享解決方法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Java實現(xiàn)統(tǒng)計文件夾下所有文件的字數(shù)
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)統(tǒng)計文件夾下所有文件的字數(shù),文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

