day2-项目一家庭收支记账软件
生活随笔
收集整理的這篇文章主要介紹了
day2-项目一家庭收支记账软件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目一家庭收支記賬軟件總結
- .nextLine()方法返回的是Enter鍵之前的所有字符,它是可以得到帶空格的字符串的。
- .next()會自動消去有效字符前的空格,只返回輸入的字符,不能得到帶空格的字符串。
(簡單點說,next我只要字,nextLine我啥都要)
- .hasNext()表示是否有輸入數據。
- .length()求一個字符串的長度
- .Integer.parseInt(): String 強制轉換為 Int 類型
- .toUpperCase() :字符串中字符全部轉為大寫
- 異常控制
???try {
// Integer.parseInt(): String 強制轉換為 Int 類型
???????n = Integer.parseInt(str);
???????break;
???????} catch (NumberFormatException e) {
????????????System.out.print("數字輸入錯誤,請重新輸入:");
???????}
try catch 錯誤處理;
執?規則:?先執?try中的代碼 如果拋出異常會由catch去捕獲并執? 如果沒有發?異常 catch去捕獲會被忽略掉 但是不管有沒有異常最后都會執?。
try 語句使你能夠測試代碼塊中的錯誤。
catch 語句允許你處理錯誤。
throw 語句允許你創建?定義錯誤。(拋出錯誤)
finally 使你能夠執?代碼,在 try 和 catch 之后,?論結果如何。
代碼流程:
try{ 代碼塊;
代碼 throw “字符” //拋出異常
}catch(參數){ //抓住throw 拋出的錯誤
// 處理錯誤并執?
}finally{ // ?論結果如何還是繼續執?
}
工具類:
import java.util.Scanner; /** Utility工具類: 將不同的功能封裝為方法,就是可以直接通過調用方法使用它的功能,而無需考慮具體的功能實現細節。 */ public class Tools {private static Scanner scan = new Scanner(System.in);/**用于界面菜單的選擇。該方法讀取鍵盤,如果用戶鍵入’1’-’4’中的任意字符,則方法返回。返回值為用戶鍵入字符。*/public static char readShuRu() {char c;for (; ; ) {//該方法從鍵盤讀取一個不超過1位長度的整數String str = readKeyBoard(1);c = str.charAt(0);if (c != '1' && c != '2' && c != '3' && c != '4') {System.out.print("選擇錯誤,請重新輸入:");} else break;}return c;}/**用于收入和支出金額的輸入。該方法從鍵盤讀取一個不超過4位長度的整數,并將其作為方法的返回值。*/public static int readJinE() {int n;for (; ; ) {String str = readKeyBoard(4);//異常控制try {// Integer.parseInt(): String 強制轉換為 Int 類型n = Integer.parseInt(str);break;} catch (NumberFormatException e) {System.out.print("數字輸入錯誤,請重新輸入:");}}return n;}/**用于收入和支出說明的輸入。該方法從鍵盤讀取一個不超過8位長度的字符串,并將其作為方法的返回值。*/public static String readShuoMing() {String str = readKeyBoard(8);return str;}/**用于確認選擇的輸入。該方法從鍵盤讀取‘Y’或’N’,并將其作為方法的返回值。*/public static char readQueRen() {char c;for (; ; ) {// 字符串.toUpperCase() :字符串中字符全部轉為大寫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 (scan.hasNext()) {line = scan.nextLine();if (line.length() < 1 || line.length() > limit) {System.out.print("輸入長度(不大于" + limit + ")錯誤,請重新輸入:");continue;}break;}return line;} }源代碼:
/* 家庭收支記錄系統 */public class FamilyNote{public static void main(String[] args){boolean isFlag = true;//用于記錄用戶的收入和支出的詳情String noTes = "收 支\t賬戶金額\t收支金額\t說 明\n";//初始金額int monoy = 0; while(isFlag){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 shuRu = Tools.readShuRu();switch(shuRu){case '1'://System.out.println(" 1 收支明細");System.out.println("---------家庭收支記錄---------");//System.out.println("收 支\t賬戶金額\t收支金額\t說 明\n");System.out.println(noTes);System.out.println("------------------------------");break;case '2'://System.out.println(" 2 登記收入");System.out.println("本次的收入金額: ");int jinE1 = Tools.readJinE();System.out.println("本次的收入說明: ");String shuoMing1 = Tools.readShuoMing();//處理余額monoymonoy += jinE1;//處理notesnoTes += "收 入\t" + monoy + "\t\t" +jinE1 + "\t\t" + shuoMing1 + "\n";System.out.println("---------登記完成!---------\n");break;case '3'://System.out.println(" 3 登記支出");System.out.println("本次的支出金額: ");int jinE2 = Tools.readJinE();System.out.println("本次的支出說明: ");String shuoMing2 = Tools.readShuoMing();//處理余額monoyif(jinE2 <= monoy){monoy -= jinE2;}else{System.out.println("支出超出額度,支付失敗!!!");}//處理notesnoTes += ("支 出\t"+monoy+"\t\t"+jinE2+"\t\t"+shuoMing2+"\n");break;case '4'://System.out.println(" 4 退 出");System.out.println("確認是否退出(Y/N):");char isChu = Tools.readQueRen();if(isChu == 'Y'){isFlag = false;}else{//break;}}}} }總結
以上是生活随笔為你收集整理的day2-项目一家庭收支记账软件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python操作主流数据库
- 下一篇: 您的高品质生活还差一个不卡顿的网络