day10记账软件
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
package account;
import java.util.Scanner;
/**
- 家庭收支記賬軟件
- 分析:
- 1.記錄每次的收支金額 int money
- 2.記錄收支總金額 int sum
- 3.記錄人每次收支說明 String note
- 4.記錄每次收支情況容器 String[] dbs
- 5.記錄每次收支情況 String db
- 6.定義一個臨時數(shù)組 String[] newArr
- 1.定義需要的變量,并初始化
- 2.因為我們不知道要操作多少次,使用到循環(huán),while循環(huán)語句:while(true){}
- 3.在循環(huán)體內(nèi)部,打印歡迎頁面
- 4.定義鍵盤錄入對象,選擇相應功能,可以用判斷語句,if和switch語句都可以,選擇switch
- 5.收支明細功能,如果這個數(shù)組為空,提示沒有明細;否則遍歷這個數(shù)據(jù),將數(shù)組進行打印輸出
- 6.收入登記,先鍵盤錄入數(shù)據(jù),記行數(shù)據(jù)拼接,存入數(shù)據(jù)
- 7.支出登記,先鍵盤錄入數(shù)據(jù),記行數(shù)據(jù)拼接,存入數(shù)據(jù)
- 8.執(zhí)行程序退出
- @author Shark
*/ public class FamilyAccount { public static void main(String[] args) {
//1.定義需要的變量,并初始化//記錄每次的收支金額 int money = 0;//記錄收支總金額 int sum = 0;//記錄人每次收支說明 String note;//記錄每次收支情況容器(數(shù)組版數(shù)據(jù)庫) String[] dbs = new String[0];//記錄每次收支情況String db;//定義一個臨時數(shù)組 String[] newArr;Scanner sc = new Scanner(System.in);//2.因為我們不知道要操作多少次,使用到循環(huán),while循環(huán)語句:while(true){}while(true) {//3.在循環(huán)體內(nèi)部,打印歡迎頁面System.out.println("---------------歡迎登陸家庭收支記賬軟件---------------");System.out.println("1、收支明細");System.out.println("2、登記收入");System.out.println("3、登記支出");System.out.println("4、退出軟件");//4.定義鍵盤錄入對象,選擇相應功能,可以用判斷語句,if和switch語句都可以,選擇switchSystem.out.println("請選擇選項:");int num = sc.nextInt();switch(num) {case 1://5.收支明細功能,如果這個數(shù)組為空,提示沒有明細;否則遍歷這個數(shù)據(jù),將數(shù)組進行打印輸出if (dbs.length == 0) {System.out.println("暫無收支明細!");} else {// 定義明細標頭 在字符串中有一些指定的轉義字符 System.out.println("收支情況\t\t收支金額\t\t賬戶金額\t\t收支說明");for (int i = 0; i < dbs.length; i++) {System.out.println(dbs[i]);}}break;case 2://6.收入登記,先鍵盤錄入數(shù)據(jù),記行數(shù)據(jù)拼接,存入數(shù)據(jù)System.out.println("請輸入收入金額");//利用money變量記錄每次存儲的金額money = sc.nextInt();//不要忘記,將每次的金額記錄到總金額中sum += money;System.out.println("請輸入收入說明");//利用note變量記錄每次存儲的金額說明note = sc.next();//記行數(shù)據(jù)拼接,格式要和標頭對其db = "收入\t\t" + money + "\t\t" + sum + "\t\t" + note;//如何進行插入,根據(jù)數(shù)據(jù)庫的長度,定義一個比數(shù)據(jù)庫數(shù)組長度+1的數(shù)組newArr = new String[dbs.length+1];//看數(shù)據(jù)庫長度是否為0,如果為0,數(shù)據(jù)庫數(shù)組長度不為零 for (int i = 0; i < dbs.length; i++) {newArr[i] = dbs[i];} //因為老數(shù)組比新數(shù)組長度-1,將新的數(shù)據(jù)放到新數(shù)組中最大索引位置newArr[newArr.length -1] = db;//}//得到一個新的數(shù)據(jù)庫,將新的數(shù)據(jù)庫數(shù)組賦值給dbsdbs = newArr;System.out.println("數(shù)據(jù)插入成功");break;case 3://支出登記,先鍵盤錄入數(shù)據(jù),記行數(shù)據(jù)拼接,存入數(shù)據(jù)System.out.println("請輸入支出金額");//利用money變量記錄每次存儲的金額money = sc.nextInt();//不要忘記,將每次的金額記錄到總金額中sum -= money;System.out.println("請輸入支出說明");//利用note變量記錄每次存儲的金額說明note = sc.next();//記行數(shù)據(jù)拼接,格式要和標頭對其db = "支出\t\t" + money + "\t\t" + sum + "\t\t" + note;//如何進行插入,根據(jù)數(shù)據(jù)庫的長度,定義一個比數(shù)據(jù)庫數(shù)組長度+1的數(shù)組newArr = new String[dbs.length+1];//看數(shù)據(jù)庫長度是否為0,如果為0,數(shù)據(jù)庫數(shù)組長度不為零 /*if(dbs.length == 0) {newArr[0] = db;} else {*/for (int i = 0; i < dbs.length; i++) {newArr[i] = dbs[i];} //因為老數(shù)組比新數(shù)組長度-1,將新的數(shù)據(jù)放到新數(shù)組中最大索引位置newArr[newArr.length -1] = db;//}//得到一個新的數(shù)據(jù)庫,將新的數(shù)據(jù)庫數(shù)組賦值給dbsdbs = newArr;System.out.println("數(shù)據(jù)插入成功");break;case 4://執(zhí)行程序退出/*break;//結束switch語句break;//結束while死循環(huán)//在方法的返回值為void的時候,return后面不需要寫返回值,return關鍵字可以忽略不寫,結束方法*/ return;//結束方法,讓方法出棧, default: System.out.println("您輸入的選項有誤,請重新輸入"); break; } } } }
轉載于:https://my.oschina.net/architectliuyuanyuan/blog/3038963
總結
- 上一篇: 后台返回给前端数据拆分成三级菜单
- 下一篇: nb-iot简介【转】