? ?一.需求分析:
除了整數以外,還要支持真分數的四則運算,真分數的運算,例如:1/6 + 1/8 = 7/24
運算符為 +, ?, ×, ÷
并且要求能處理用戶的輸入,并判斷對錯,打分統計正確率。
要求能處理用戶輸入的真分數, 如 1/2, 5/12 等
使用 -n 參數控制生成題目的個數,例如執行下面命令將生成10個題目。
?
?二.功能設計:
根據所寫的代碼,隨機生成數字以及運算符號,并且組合成式子。
能生成分數,并且是最簡分數。
輸入計算結果后,如正確,輸出正確,錯誤,則輸出正確答案,并且能統計出錯的題目個數。
? ?
三.設計實現:
此次實驗,設置了4個class,主類--Main用來調用其他類,負責輸入輸出,判定結果以及統計錯誤個數
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 類--result 計算
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 類--fenshu 求最大公約數
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 類--qqq用來生成的式子
?
四.代碼說明:
主要的是main和qqq兩個類
main類:用循環語句來計算錯誤的個數,當number=0時,則跳出循環錯誤個數加1.
import java.util.Scanner; public class Main {static randomqqq rand = new randomqqq();static result end = new result();public static void main(String[] args) {int number = 0;int falseNum = 0;String userresult, trueresult, fenzi, fenmu;Scanner in = new Scanner(System.in);System.out.print("輸入隨機生成的式子個數:");number = in.nextInt();for (; number != 0; number--) {rand.randTest();System.out.println("你輸入的答案是:");Scanner result = new Scanner(System.in);userresult = result.nextLine();trueresult = Integer.toString(rand.result);if (rand.m == 0 && rand.randNum != 3) {trueresult = Integer.toString(rand.result);} else {trueresult = Integer.toString(rand.fenzi) + "/" + Integer.toString(rand.fenmu);}if (userresult.equals(trueresult)) {System.out.println("答案正確!");} else {falseNum++;System.out.println("答案錯誤! 正確的答案:" + trueresult);}}System.out.println("錯誤的個數:" + falseNum);} }
?
類qqq:選擇參數,計算出式子
int temp1 = yuefen.getnumber(a, c);int temp2 = yuefen.getnumber(b, d);int temp4 = yuefen.getnumber(a, b); int fz = end.up(a, b, c, d, randNum);int fm = end.fenmu(a, b, c, d, randNum);int temp3 =yuefen.getnumber(fz,fm) ;switch (m) {case 0:if (b == 0 && randNum == 3)System.out.println(b + " " + ch[randNum] + " " + a + " " + "=");elseSystem.out.println(a + " " + ch[randNum] + " " + b + " " + "=");break;case 1:System.out.println(a / temp1 + "/" + c / temp1 + " " + ch[randNum] + " " + b / temp2 + "/" + d / temp2 + " " + "=");break;}if (m == 0 && randNum != 3)result = end.result1(a, b, randNum);else if (m == 0 && randNum == 3) {fenzi = a / temp4;fenmu = b / temp4;} else {fenzi = fz/ temp3;fenmu = fm / temp3;}
?
?
五.測試運行:
PSP:
PSP2.1 | Personal Software Process Stages | Time (%) Senior Student | Time (%) |
Planning | 計劃 | 8 | 5 |
· Estimate | 估計這個任務需要多少時間 | 20 | 5 |
Development | 開發 | 25 | 20 |
· Analysis | 需求分析 (包括學習新技術) | 5 | 5 |
· Design Spec | 生成設計文檔 | 5 | 5 |
· Design Review | 設計復審 | 0 | 0 |
· Coding Standard | 代碼規范 | 3 | 3 |
· Design | 具體設計 | 10 | 15 |
· Coding | 具體編碼 | 50 | 40 |
· Code Review | 代碼復審 | 5 | 5 |
· Test | 測試(自我測試,修改代碼,提交修改) | 10 | 15 |
Reporting | 報告 | 5 | 5 |
· | 測試報告 | 1 | 1 |
· | 計算工作量 | 2 | 5 |
· | 并提出過程改進計劃 | 3 | 2 |
?
實驗總結:
?在編程之前,需要思考好可能忽略的問題以及可能出現的問題,比如此次實驗,分數需要最簡化,分數中,分母不為0的情況。計算錯誤的題數時,如何設計循環語句使其能夠正確計算個數,生成式子時可能會出現錯誤的式子,式子的格式要規范等問題。有問題自己積極思考,思考不出來呢就問百度,找同學。 編程需要一步一步訓練,才能有所進步。
?