![](https://img-blog.csdnimg.cn/f1f342772ef14030be5d30e510252f22.png)
//工具
import java.util.Scanner;
public class Utility
{
static Scanner in = new Scanner(System.in);
public static char readMenuSelection()
{
char c;
while(true)
{
String str = readKeyBoard(1);
c = str.charAt(0);
if(c != '1' && c != '2' && c != '3' && c != '4')
{
System.out.println("输入错误");
}
else
{
break;
}
}
return c;
}
public static int readNumber()
{
int n;
while(true)
{
String str = readKeyBoard(4);
try
{
n = Integer.parseInt(str);
break;
}
catch (NumberFormatException e)
{
System.out.println("输入错误,请重新输入");
}
}
return n;
}
public static String readString()
{
String str = readKeyBoard(8);
return str;
}
public static char readConfirmSelection()
{
char c;
while(true)
{
String str = readKeyBoard(1).toUpperCase();
c = str.charAt(0);
if(c == 'Y' || c == 'N')
{
break;
}
else
{
System.out.println("选择错误,请重新输入");
}
}
return c;
}
private static String readKeyBoard(int limit)
{
String line = "";
while(in.hasNext())
{
line = in.nextLine();
if(line.length() < 1 || line.length() > limit)
{
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
else
{
break;
}
}
return line;
}
}
import java.util.Scanner;
public class Utility
{
static Scanner in = new Scanner(System.in);
public static char readMenuSelection()
{
char c;
while(true)
{
String str = readKeyBoard(1);
c = str.charAt(0);
if(c != '1' && c != '2' && c != '3' && c != '4')
{
System.out.println("输入错误");
}
else
{
break;
}
}
return c;
}
public static int readNumber()
{
int n;
while(true)
{
String str = readKeyBoard(4);
try
{
n = Integer.parseInt(str);
break;
}
catch (NumberFormatException e)
{
System.out.println("输入错误,请重新输入");
}
}
return n;
}
public static String readString()
{
String str = readKeyBoard(8);
return str;
}
public static char readConfirmSelection()
{
char c;
while(true)
{
String str = readKeyBoard(1).toUpperCase();
c = str.charAt(0);
if(c == 'Y' || c == 'N')
{
break;
}
else
{
System.out.println("选择错误,请重新输入");
}
}
return c;
}
private static String readKeyBoard(int limit)
{
String line = "";
while(in.hasNext())
{
line = in.nextLine();
if(line.length() < 1 || line.length() > limit)
{
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
else
{
break;
}
}
return line;
}
}
//主函数
class program
{
public static void main(String args[])
{
boolean isFlag = true;
int balance = 10000;
String InInfo = "";
String OutInfo = "";
while(isFlag == true)
{
System.out.println("---------------记账软件---------------");
System.out.println(" 1.收支明细 ");
System.out.println(" 2.登记收入 ");
System.out.println(" 3.登记支出 ");
System.out.println(" 4. 退 出\n ");
System.out.print(" 请选择(1-4): ");
char selection = Utility.readMenuSelection();
switch (selection)
{
case '1':
{
System.out.println("\t当前收支明细记录\t");
System.out.println("收支\t账户金额\t收支金额\t说明\n");
System.out.println(InInfo);
System.out.println(OutInfo);
System.out.println("\t当前收支明细记录\t");
break;
}
case '2':
{
System.out.println("\t本次收入金额\t");
int income = Utility.readNumber();
if(income > 0)
{
balance = income + balance;
System.out.println("\t本次收入类型\t");
String InText = Utility.readString();
InInfo = InInfo + "收入\t" + balance + "\t" + "income" + "\t" + InText + "\n";
}
else
{
System.out.println("金额错误,请重新输入");
}
break;
}
case '3':
{
System.out.println("\t本次支出金额\t");
int outcome = Utility.readNumber();
balance = balance - outcome;
System.out.println("\t本次支出类型\t");
String OutText = Utility.readString();
OutInfo = OutInfo + "支出\t" + balance + "\t" + "outcome" + "\t" + OutText + "\n";
break;
}
case '4':
{
System.out.println("您是否要退出");
char isExit = Utility.readConfirmSelection();
if(isExit == 'Y')
{
isFlag = false;
}
break;
}
}
}
}
}
class program
{
public static void main(String args[])
{
boolean isFlag = true;
int balance = 10000;
String InInfo = "";
String OutInfo = "";
while(isFlag == true)
{
System.out.println("---------------记账软件---------------");
System.out.println(" 1.收支明细 ");
System.out.println(" 2.登记收入 ");
System.out.println(" 3.登记支出 ");
System.out.println(" 4. 退 出\n ");
System.out.print(" 请选择(1-4): ");
char selection = Utility.readMenuSelection();
switch (selection)
{
case '1':
{
System.out.println("\t当前收支明细记录\t");
System.out.println("收支\t账户金额\t收支金额\t说明\n");
System.out.println(InInfo);
System.out.println(OutInfo);
System.out.println("\t当前收支明细记录\t");
break;
}
case '2':
{
System.out.println("\t本次收入金额\t");
int income = Utility.readNumber();
if(income > 0)
{
balance = income + balance;
System.out.println("\t本次收入类型\t");
String InText = Utility.readString();
InInfo = InInfo + "收入\t" + balance + "\t" + "income" + "\t" + InText + "\n";
}
else
{
System.out.println("金额错误,请重新输入");
}
break;
}
case '3':
{
System.out.println("\t本次支出金额\t");
int outcome = Utility.readNumber();
balance = balance - outcome;
System.out.println("\t本次支出类型\t");
String OutText = Utility.readString();
OutInfo = OutInfo + "支出\t" + balance + "\t" + "outcome" + "\t" + OutText + "\n";
break;
}
case '4':
{
System.out.println("您是否要退出");
char isExit = Utility.readConfirmSelection();
if(isExit == 'Y')
{
isFlag = false;
}
break;
}
}
}
}
}