1 public class FamliyAccount 2 { 3 4 public static void main(String[] args) 5 { 6 // TODO Auto-generated method stub 7 8 boolean isFlag = true; 9 //用于记录用户的收入和支出的详情 10 String details = "收支\t账户金额\t\t收支金额\t\t说 明\n"; 11 //初始金额 12 int balance = 10000; 13 while(isFlag) 14 { 15 System.out.println("-----------------家庭收支记账本-----------------\n"); 16 System.out.println(" 1-收支明细"); 17 System.out.println(" 2-登记收入"); 18 System.out.println(" 3-登记支出"); 19 System.out.println(" 4-退 出\n"); 20 System.out.print(" 请选择(1--4): "); 21 22 23 24 25 Scanner sanner = new Scanner(System.in); 26 String keyBoard = sanner.next(); 27 char c= keyBoard.charAt(0); 28 29 switch(c) 30 { 31 case ‘1‘: 32 System.out.println("1-收支明细"); 33 System.out.println("------------------当前收支明细记录------------------"); 34 35 System.out.println(details); 36 37 System.out.println("---------------------------------------------------"); 38 39 40 41 break; 42 43 case ‘2‘: 44 System.out.println("2-登记收入"); 45 System.out.print("本次收入金额:"); 46 int money = sanner.nextInt(); 47 48 System.out.print("本次收入说明:"); 49 String info = sanner.next(); 50 51 52 53 //处理balance 54 balance += money; 55 56 //处理details 57 details +=("收入\t"+ balance +"\t\t" + money +"\t\t"+info+"\n"); 58 System.out.println("---------------------登记完成----------------------"); 59 60 61 62 break; 63 case ‘3‘: 64 System.out.println("3-登记支出"); 65 System.out.print("本次支出金额:"); 66 67 int moneyZc = sanner.nextInt(); 68 69 System.out.print("本次支出说明:"); 70 String infoZc = sanner.next(); 71 72 //处理balance 73 if(balance >= moneyZc) 74 { 75 balance -= moneyZc; 76 //处理details 77 details +=("支出\t"+ balance +"\t\t" + moneyZc +"\t\t"+infoZc+"\n"); 78 } 79 else 80 { 81 System.out.println("支出超出额度,支付失败!!!"); 82 } 83 84 System.out.println("---------------------登记完成----------------------"); 85 86 87 break; 88 case ‘4‘: 89 System.out.print("确认是否退出?(Y/N):"); 90 for(;;) 91 { 92 93 String str = sanner.next(); 94 char exit = str.charAt(0); 95 if(exit == ‘Y‘||exit ==‘y‘) 96 { 97 98 System.out.println("已退出"); 99 break; 100 } 101 else if(exit == ‘N‘|| exit ==‘n‘) 102 { 103 System.out.println("已取消退出"); 104 } 105 else 106 { 107 System.out.println("输入有误,请重新输入:"); 108 // break; 109 } 110 } 111 112 113 } 114 115 116 117 118 119 120 121 } 122 123 } 124 125 }