Github地址:https://github.com/brucezhangi/sizeyunsuan.git
项目要求
1、需求分析
*
*为了让小学生得到充分锻炼,每个练习题至少要包含2种运算符。同时,由于小学生没有分数与负数的概念,你所出的练习题在运算过程中不得出现负数与非整数,比如不能出 3/5+2=2.6,2-5+10=7等算式。
*练习题生成好后,将学号
*当程序接收的参数为3时,以下为输出文件示例。
4、测试运行
eclipse测试:
5、核心代码
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import java.util.Scanner; public class sizeyunsuan { private static int fNum, sNum, tNum, result; private static String firstOperator, secondOperator; private final static String[] OPERATOR = { "+", "-", "*", "/" }; private static int ac=1; public static void main(String[] args) { System.out.println("请输入产生题目数量:"); Scanner s = new Scanner(System.in); int a=s.nextInt(); for (int i = 0; i < a; i++) { ys(); } } public static boolean ys() { while (true) { fNum = generateRandomNum(1, 99); sNum = generateRandomNum(1, 99); tNum = generateRandomNum(1, 99); firstOperator = OPERATOR[generateRandomNum(0, 3)]; secondOperator = OPERATOR[generateRandomNum(0, 3)]; if (firstOperator.equals(secondOperator)) { continue; } try { if (secondOperator.equals(Const.Operator.multiplication) || secondOperator.equals(Const.Operator.division)) { int preResult = ys(sNum, tNum, secondOperator); if (preResult < 0) { continue; } result = ys(fNum, preResult, firstOperator); } else { int preResult = ys(fNum, sNum, firstOperator); if (preResult < 0) { continue; } result = ys(preResult, tNum, secondOperator); } } catch (Exception e) { // TODO: handle exception continue; } if (result < 0) { continue; } else { StringBuffer buffer = new StringBuffer(); buffer.append(fNum).append(firstOperator).append(sNum).append(secondOperator).append(tNum) .append("=").append(result).append(" "); System.out.println(buffer); if (writeToFile(buffer.toString())) { return true; } else { return false; } } } } public static int generateRandomNum(int min, int max) { Random random = new Random(); return random.nextInt(max - min + 1) + min; } public static boolean writeToFile(String result) { try { File f1 = new File("result.txt"); FileWriter fw = new FileWriter(f1, true); PrintWriter pw = new PrintWriter(fw); if(ac==1) { pw.println("201571030134"); ac=0; } pw.println(result); fw.flush(); fw.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } public static Integer ys(int firstNum, int secNum, String operator) throws Exception{ switch (operator) { case Const.Operator.add: { return firstNum + secNum; } case Const.Operator.subtraction: { return firstNum - secNum; } case Const.Operator.multiplication: { return firstNum * secNum; } case Const.Operator.division: { if (firstNum % secNum == 0) { return firstNum / secNum; } else { throw new Exception(""); } } default: { return null; } } } }
6、psp展示
PSP2.1 |
任务内容 |
计划完成需要的时间(h) |
实际完成需要的时间(h) |
Planning |
计划 |
0.5 |
1 |
· Estimate |
· 估计这个任务需要多少时间,并规划大致工作步骤 |
0.5 |
1 |
Development |
开发 |
19 |
24 |
·· Analysis |
需求分析 (包括学习新技术) |
5 |
6 |
· Design Spec |
· 生成设计文档 |
1 |
1 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
0.2 |
0.3 |
· Coding Standard |
代码规范 (为目前的开发制定合适的规范) |
0.2 |
0.2 |
· Design |
具体设计 |
0.6 |
0.6 |
· Coding |
具体编码 |
10 |
12 |
· Code Review |
· 代码复审 |
0.5 |
0.5 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
2 |
4 |
Reporting |
报告 |
0.8 |
1 |
·· Test Report |
· 测试报告 |
0.3 |
0.4 |
· Size Measurement |
计算工作量 |
0.2 |
0.2 |
· Postmortem & Process Improvement Plan |
· 事后总结 ,并提出过程改进计划 |
0.3 |
0.3 |
7、总结
本次设计使我印象深刻,耗时很多,由于我的JAVA基础很薄弱,没有很好的掌握语言及其应用,导致在写的过程中没有思路,丢失的内容很多,然后改起来很麻烦,设计中没能按要求完成所有任务。在以后的学习中我会继续加油,再接再厉,争取做得更出色!