《个人开发流程》——计应191第六组刘会芳
一:需求分析:
Java编写加减乘除四则运算,每次运行随机生成10道题目,每题10分。
二:具体设计:
建两个实体类,一个题目实体类Subject,一个试卷类Exam。
三:具体代码:
1 package com.exam.demo; 2 import java.util.ArrayList; import java.util.List; 3 import java.util.Random; import java.util.function.BiFunction; /** * 试卷实体类 */ 4 public class Exam { private List<Subject> subjectList; 5 public List<Subject> getSubjectList() { return subjectList; } 6 public void setSubjectList(List<Subject> subjectList) { 7 this.subjectList = subjectList; } 8 public Exam() { int count = 10; List<Subject> list = new ArrayList<Subject>(); 9 BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y; BiFunction<Integer, Integer, Integer> minus = (x, y) -> x - y; 10 BiFunction<Integer, Integer, Integer> multiple = (x, y) -> x * y; 11 BiFunction<Integer, Integer, Integer> divide = (x, y) -> x / y; 12 for (int i = 0; i < count; i++) { Subject subject = new Subject(); 13 Integer a = new Random().nextInt(99); Integer b = new Random().nextInt(99); subject.setA(a); 14 subject.setB(b); subject.setIndex(i + 1); Integer symbol = new Random().nextInt(3); 15 if (symbol == 0) { subject.setSymbol("+"); 16 subject.setAnswer(add.apply(a, b)); } 17 else if (symbol == 1) { subject.setSymbol("-"); subject.setAnswer(minus.apply(a, b)); } 18 else if (symbol == 2) { subject.setSymbol("×"); subject.setAnswer(multiple.apply(a, b)); } 19 else if (symbol == 3) { subject.setSymbol("÷"); 20 subject.setAnswer(divide.apply(a, b)); } list.add(subject); } this.subjectList = list; } 21 public static void main(String[] args) { Exam exam = new Exam();// 随机出十道题 // 查看试卷 exam.getSubjectList().forEach(subject -> { 22 System.out.println("第" + subject.getIndex() + "题:" + subject.getA() + " " + subject.getSymbol() + " " + subject.getB() + " = ? 答案:" + subject.getAnswer()); }); } } /** * 定义题目的类 * */ 23 class Subject { private Integer index; private Integer a; private Integer b; 24 private String symbol; private Integer answer; 25 public Integer getIndex() { return index; } 26 public void setIndex(Integer index) { this.index = index; } public Integer getA() { return a; } public void setA(Integer a) { this.a = a; } 27 public Integer getB() { return b; } public void setB(Integer b) { this.b = b; } public String getSymbol() { return symbol; } 28 public void setSymbol(String symbol) { this.symbol = symbol; } 29 public Integer getAnswer() { return answer; } public void setAnswer(Integer answer) { this.answer = answer; } }
四:运算结果:
运行打印结果:
第1题:67 + 62 = ? 答案:129
第2题:54 - 66 = ? 答案:-12
第3题:15 × 36 = ? 答案:540
第4题:81 + 6 = ? 答案:87
第5题:87 - 70 = ? 答案:17
第6题:83 - 80 = ? 答案:3
第7题:1 - 88 = ? 答案:-87
第8题:17 × 14 = ? 答案:238
第9题:57 × 31 = ? 答案:1767
第10题:73 + 6 = ? 答案:79
五:psp(个人软件开发流程)
psp阶段
六:总结
在刚开始这个任务的时候,大致想了一下流程,觉得没那么简单,后来实际操作过程中,确实如此。又出现了好多问题,比如之前的知识遗忘了好多,测试不顺等等,后来多次测试终于实现了需要的功能。总之,接下来的学习要好好对待,好好积累,灵活运用自己以前学过的知识,更好提升自己。

浙公网安备 33010602011771号