主菜單:


活動(dòng)流程圖?

詳細(xì)要求
子功能要求

import java.util.InputMismatchException;
import java.util.Scanner;
@SuppressWarnings("all")
public class Family_2 {
public static Scanner sc = new Scanner(System.in);
public static boolean loop = true;
public static int principal = 1000;
public static int count = 0;
public static String[] arr = new String[10];
public static void main(String[] args) {
run();
System.out.println(arr.length);
}
// 運(yùn)行方法
public static void run() {
while (loop) {
show();
System.out.println("請(qǐng)輸入你的選擇(1~4)");
int select = sc.nextInt();
selection(select);
}
}
// 主界面展示
public static void show() {
System.out.println("-------家庭收支管理系統(tǒng)-------");
System.out.println(" 1.收支明細(xì)");
System.out.println(" 2.登記收入");
System.out.println(" 3.登記支出");
System.out.println(" 4.退 出");
}
// 功能選擇
public static void selection(int select) {
if (select != 4 && count == arr.length - 1)
expansion();
switch (select) {
case 1:
prrintDetails();
break;
case 2:
registerIncome();
break;
case 3:
registerOutcome();
break;
case 4:
loop = false;
System.out.println("4.退出");
break;
}
}
// 打印明細(xì)
public static void prrintDetails() {
System.out.println("收支\t賬戶總金額\t收支金額\t\t說(shuō)明");
if (count == 0) {
System.out.println("這里空空如也");
return;
}
System.out.println("....");
for (int i = 0; i< count; i++) {
String str = arr[i];
if (str.length() >30)
str = substr(str);
System.out.println(str);
}
}
// 登記收入
public static void registerIncome() {
System.out.print("輸入你的存款金額,");
int add = scanf();
principal += add;
System.out.println("輸入你本次存款的來(lái)源");
String str = sc.next();
arr[count++] = "收入\t" + principal + "\t " + add + paddedStr(add) + str;
}
// 登記支出
public static void registerOutcome() {
System.out.println("輸入你的取款金額");
int subtract = scanf();
if (principal< subtract) {
System.out.println("余額不足");
return;
}
principal -= subtract;
System.out.println("輸入你本次取款的原因");
String str = sc.next();
System.out.println("本次說(shuō)明的長(zhǎng)度");
arr[count++] = "支出\t" + principal + "\t " + subtract + "" +paddedStr(subtract) + str;
}
// 數(shù)組擴(kuò)容
public static void expansion() {
int oldLen = arr.length;
int newLen = oldLen + (oldLen<< 1);
String[] str = new String[newLen];
System.arraycopy(arr, 0, str, 0, count + 1);
arr = str;
}
// 字符串補(bǔ)齊
public static String paddedStr(int money) {
return money >= 100 ? " \t\t" : " \t\t";
}
public static String substr(String str){
// 輸出時(shí),較長(zhǎng)的字符串轉(zhuǎn)換為 xxx...
str = str.substring(0, 30);
System.out.println("省略后的字符串");
return str+ "...";
}
// 安全輸入
public static int scanf() {
int num = 0;
System.out.println("請(qǐng)輸入0~100000的數(shù)字");
try {
num = sc.nextInt();}
catch (InputMismatchException e){
sc.next();
num = scanf();
return num;
}
// 因無(wú)其他要求,此處僅判斷金額大于 0
if (num >0)
return num;
return -1;
}
}
過(guò)程中遇到的問(wèn)題
1 public static int scanf() {
2 int num = 0;
3 while (true) {
4 System.out.println("請(qǐng)輸入0~100000的數(shù)字");
5 try {
6 num = sc.nextInt();}
7 catch (InputMismatchException e){
8 continue;
9 }
10 if (num >0)
11 return num;
}
}原本想法:如果輸入的不是數(shù)字,捕獲異常并跳過(guò)本次循環(huán),接收下一次輸入
運(yùn)行結(jié)果

4.原因
在使用Scanner中nextInt掃描時(shí),如果輸入的不是一個(gè)整數(shù),系統(tǒng)會(huì)認(rèn)為是當(dāng)前格式無(wú)法解析該數(shù)據(jù),從而導(dǎo)致該數(shù)據(jù)一直留在緩沖區(qū),導(dǎo)致之后的輸入異常。
5.解決方案
1.將緩沖區(qū)的數(shù)據(jù)讀取
public static int scanf() {
int num = 0;
while (true) {
System.out.println("請(qǐng)輸入0~100000的數(shù)字");
try {
num = sc.nextInt();}
catch (InputMismatchException e){
// 下面的讀取將會(huì)把sc.nextInt中的錯(cuò)誤輸入讀取
sc.next();
continue;
}
if (num >0)
return num;
}
}2.關(guān)閉上一個(gè)Scanner,重新創(chuàng)建,會(huì)刷新緩沖區(qū)
public static int scanf() {
int num = 0;
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入0~100000的數(shù)字");
try {
num = sc.nextInt();}
catch (InputMismatchException e){
continue;
}
if (num >0)
return num;
}
}3.不建議在循環(huán)中使用try catch,上述代碼可改為:?
public static int scanf() {
int num = 0;
System.out.println("請(qǐng)輸入0~100000的數(shù)字");
try {
num = sc.nextInt();}
catch (InputMismatchException e){
return num = scanf();
}
if (num >0)
return num;
}你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
標(biāo)題名稱(chēng):java方法、數(shù)組、控制語(yǔ)句綜合小案例-創(chuàng)新互聯(lián)
分享鏈接:http://chinadenli.net/article44/diicee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、商城網(wǎng)站、軟件開(kāi)發(fā)、標(biāo)簽優(yōu)化、網(wǎng)站策劃、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容