家庭记账软件
FamilyAccount
int balance = 10000;//生活基本金额
String details = "描述\t账户金额\t\t流水金额\t\t说 明\n";
boolean loopFlag = true;//控制循环的标记
do {
System.out.println("\n-----------------家庭收支记账软件-----------------\n");
System.out.println("\t\t\t\t 1 收支明细");
System.out.println("\t\t\t\t 2 登记收入");
System.out.println("\t\t\t\t 3 登记支出");
System.out.println("\t\t\t\t 4 退 出");
System.out.println();
System.out.print("\t\t\t\t 请选择(1-4):");
//此方法接收 1-4 任意字符
char ch = Utility.readMenuSelection();
switch (ch) {
//收支明细
case '1':
System.out.println("-----------------当前收支明细记录-----------------");
System.out.println(details);
break;
//登记收入
case '2':
System.out.print("本次收入金额:");
int amount1 = Utility.readNumber();//接收3位数的整数
System.out.print("本次收入说明:");
String str1 = Utility.readString();//收入的描述
balance += amount1;
details += "收入\t" + balance + "\t\t" + amount1 + "\t\t\t" + str1 + "\n";
break;
//登记支出
case '3':
System.out.print("本次支出金额:");
int num2 = Utility.readNumber();//接收3位数的整数
System.out.print("本次支出说明:");
String str2 = Utility.readString();//支出的描述
balance -= num2;
details += "支出\t" + balance + "\t\t" + num2 + "\t\t" + str2 + "\n";
break;
//退出
case '4':
System.out.print("请确认是否退出(y/n):");
char yn = Utility.readConfirmSelection();
if (yn == 'Y') {
loopFlag = false;
}
break;
}
} while (loopFlag);
Utility
package com.dream;
import java.util.*;
public class Utility {
private static Scanner scanner = new Scanner(System.in);
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1);
c = str.charAt(0);
if (c != '1' && c != '2' && c != '3' && c != '4') {
System.out.print("选择错误,请重新输:");
} else break;
}
return c;
}
public static int readNumber() {
int n;
for (; ; ) {
String str = readKeyBoard(4);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static String readString() {
String str = readKeyBoard(8);
return str;
}
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
private static String readKeyBoard(int limit) {
String line = "";
while (scanner.hasNext()) {
line = scanner.nextLine();
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
标签:String,家庭,System,break,println,记账,print,软件,out
From: https://www.cnblogs.com/yimengxunchen/p/17252336.html