packageproject7;importjava.util.Scanner;publicclass bbb {publicstaticvoidmain(String[] args){int count =0;int sumnum =10000;//定义初始钱数10000String[][] arr =newString[100][4];//定义二维数组,用于存放键盘输入int a =0;//定义全局明细表条数while(true){System.out.println("-------------------家庭收支记账软件---------------------\n");System.out.println(" 1.收支明细");System.out.println(" 2.登记收入");System.out.println(" 3.登记支出");System.out.println(" 4.退 出\n");System.out.print("请选择(1-4):");char c =Utility.readMenuSelection();switch(c){//将数组遍历,输出为记录表格式case'1':System.out.println("-------------------收支明细记录表-----------------------");System.out.println("收支\t\t账户总金额\t\t收支金额\t\t说 明");for(int i =0; i < arr.length; i++){if(arr[i][0]!=null){System.out.println(arr[i][0]+"\t\t\t"+ arr[i][1]+"\t\t"+ arr[i][2]+"\t\t\t"+ arr[i][3]);}}break;//将用户想要存入具体和金额说明存入数组 case'2':Scanner sc =newScanner(System.in);System.out.println("请输入想存入收入条数");int j = sc.nextInt();int n =1;for(int i = a; i < j + a; i++){System.out.println("请输入"+ n +"次收入的金额:");int num =Utility.readNumber();System.out.println("请输入"+ n +"次收入的说明:");String info =Utility.readString();
arr[i][0]="收入";
sumnum += num;
arr[i][1]=Integer.toString(sumnum);
arr[i][2]=Integer.toString(num);
arr[i][3]= info;
n++;}
a += j;break;case'3'://将用户想要支出具体和金额说明存入数组 Scanner sd =newScanner(System.in);System.out.println("请输入想存入支出条数");int k = sd.nextInt();int m =1;for(int i = a; i < k + a; i++){System.out.println("请输入"+ m +"次支出的金额:");int num =Utility.readNumber();System.out.println("请输入"+ m +"次支出的说明:");String info =Utility.readString();
arr[i][0]="支出";
sumnum -= num;
arr[i][1]=Integer.toString(sumnum);
arr[i][2]=Integer.toString(num);
arr[i][3]= info;
m++;}
a += k;break;case'4':System.out.println("确认是否退出(Y/N)?:");char c1 =Utility.readConfirmSelection();if(c1 =='Y'){System.out.println("拜拜!退出成功!");return;}else{break;}}}}}
引用的Utility工具类
packageproject7;importjava.util.Scanner;/**
Utility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/publicclassUtility{privatestaticScanner scanner =newScanner(System.in);/**
用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
*/publicstaticcharreadMenuSelection(){char c;for(;;){String str =readKeyBoard(1);
c = str.charAt(0);if(c !='1'&& c !='2'&& c !='3'&& c !='4'){System.out.print("选择错误,请重新输入:");}elsebreak;}return c;}/**
用于收入和支出金额的输入。该方法从键盘读取一个不超过4位长度的整数,并将其作为方法的返回值。
*/publicstaticintreadNumber(){int n;for(;;){String str =readKeyBoard(4);try{
n =Integer.parseInt(str);break;}catch(NumberFormatException e){System.out.print("数字输入错误,请重新输入:");}}return n;}/**
用于收入和支出说明的输入。该方法从键盘读取一个不超过8位长度的字符串,并将其作为方法的返回值。
*/publicstaticStringreadString(){String str =readKeyBoard(8);return str;}/**
用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
*/publicstaticcharreadConfirmSelection(){char c;for(;;){String str =readKeyBoard(1).toUpperCase();
c = str.charAt(0);if(c =='Y'|| c =='N'){break;}else{System.out.print("选择错误,请重新输入:");}}return c;}privatestaticStringreadKeyBoard(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;}}