編寫一個程序的步驟


在day04_javase的視頻中,感覺這個老師講的是超級詳細,容易懂得怎么樣做,簡直是手把手寫注釋

先一個程序:

1先分析你這個程序流程

2.在寫代碼錢用注釋寫出每一個步驟執行什么(大體的框架)

3.安裝注釋在下面一行一行寫代碼,清晰明了無比

(由於那個需求分析的ppt插入不進來,所以一般人還是不太看得懂我說的)

1.先分析這個程序的流程

2.用文字注釋寫在編寫的代碼行,

3.根據注釋寫出代碼

 1 package day04_javase;
 2 /**
 3  * 簡單的家庭收支統計系統
 4  *
 5  */
 6 public class FamilyAccount {
 7 
 8     public static void main(String[] args) {
 9         //聲明余額變量的初始值是10000
10         int balance=10000;
11         //聲明記賬本,初始值是表頭
12         String detail="收支\t 賬戶金額\t 收支金額\t 說  明\n";
13         //聲明布爾值,用來做循環條件
14         Boolean loopFlag=true;
15         //1在循環中,打印主菜單
16         while(loopFlag){
17             System.out.println("\n*******************家庭收支記賬軟件******************\n");
18             System.out.println("");
19             System.out.println("            1.收支明細");
20             System.out.println("            2.登記收入");
21             System.out.println("            3.等級支出");
22             System.out.println("        4.退出\n");
23             System.out.println("");
24             System.out.print("請輸入您的選擇(1-4):");
25         //2獲取用戶的鍵盤輸入utility.readMenuSelection();
26             char userInput=Utility.readMenuSelection();
27         //3獲取用戶輸入的分支
28             switch(userInput){
29             //用戶輸入1時,,打印記賬本;
30             case '1':
31                 System.out.println("****************當前收支記錄****************");
32                 System.out.println(detail);
33                 System.out.println("*******************************************");
34                 break;
35             //用戶輸入2時,登記收入
36             case'2':
37                 System.out.print("請輸入您收入的金額:");
38                 //用戶輸入金額用int money保存
39                 int money=Utility.readNumber();
40                 System.out.print("請輸入收入的來源:");
41                 String Info=Utility.readString();
42                 //余額的增加,本來的加上收入的等於最新的余額
43                 balance+=money;
44                 //聲明一個字符串,把當前的的操作細節拼接成一個字符串
45                 String string="收入"+"\t"+balance+"\t"+money+"\t"+Info+"\n";
46                 //把這個string字符串拼接到detail上面去
47                 detail+=string;
48                 System.out.println("************************本次錄入完成***************");
49                 break;
50             //用戶輸入3時,登記支出
51                 case'3':
52                         System.out.print("請輸入您支出的金額:");
53                         //用戶輸入金額用int money保存
54                         int money1=Utility.readNumber();
55                         System.out.print("請輸入支出的用途:");
56                         String Info1=Utility.readString();
57                         //余額的增加,本來的加上收入的等於最新的余額
58                         balance-=money1;
59                         //聲明一個字符串,把當前的的操作細節拼接成一個字符串
60                         String string1="支出"+"\t"+balance+"\t"+money1+"\t"+Info1+"\n";
61                         //把這個string字符串拼接到detail上面去
62                         detail+=string1;
63                         System.out.println("************************本次錄入完成***************");
64                         break;
65                 
66             //用戶輸入4時,設置布爾值變量為假,退出程序
67             case'4':
68                 System.out.print("是否要退出?請選擇(Y/N)");
69                 char confirm=Utility.readConfirmSelection();
70                 if(confirm=='y'){
71                 loopFlag=false;
72                 }
73                 break;
74             }
75         
76         }
77     }
78 
79 }
 1 package day04_javase;
 2 import java.util.*;
 3 
 4 public class Utility {
 5     private static Scanner scanner = new Scanner(System.in);
 6     
 7     public static char readMenuSelection() {
 8         char c;
 9         for (; ; ) {
10             String str = readKeyBoard(1);
11             c = str.charAt(0);
12             if (c != '1' && c != '2' && c != '3' && c != '4') {
13                 System.out.print("選擇錯誤,請重新輸入:");
14             } else break;
15         }
16         return c;
17     }
18 
19     public static int readNumber() {
20         int n;
21         for (; ; ) {
22             String str = readKeyBoard(4);
23             try {
24                 n = Integer.parseInt(str);
25                 break;
26             } catch (NumberFormatException e) {
27                 System.out.print("數字輸入錯誤,請重新輸入:");
28             }
29         }
30         return n;
31     }
32 
33     public static String readString() {
34         String str = readKeyBoard(8);
35         return str;
36     }
37 
38     public static char readConfirmSelection() {
39         char c;
40         for (; ; ) {
41             String str = readKeyBoard(1).toUpperCase();
42             c = str.charAt(0);
43             if (c == 'Y' || c == 'N') {
44                 break;
45             } else {
46                 System.out.print("選擇錯誤,請重新輸入:");
47             }
48         }
49         return c;
50     }
51 
52     private static String readKeyBoard(int limit) {
53         String line = "";
54 
55         while (scanner.hasNext()) {
56             line = scanner.nextLine();
57             if (line.length() < 1 || line.length() > limit) {
58                 System.out.print("輸入長度(不能大於" + limit + ")錯誤,請重新輸入:");
59                 continue;
60             }
61             break;
62         }
63 
64         return line;
65     }
66 }
Utility

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM