软件工程第二次作业

题目要求:

  • 请编写一个能自动生成小学四则运算题目的 “软件”。
  • 让程序能接受用户输入答案,并判定对错。
  • 最后给出总共 对/错 的数量。

需求分析:

基本功能:

  1. 能够进行计算基本加、减、乘、除
  2. 系统能够自己出题
  3. 给/不给出答案都能够判断对错
  4. 统计对错数量

扩展功能:

  1. 用户可以自行对数值进行编辑
  2. 统计出加、减、乘、除的对错情况,用户可以根据自己的情况在自己薄弱的题目上多练习

设计:

  1. 首先编程语言上本人采用JAVA
  2. 为了方便用户的使用,采用面向对象的程序设计
  3. 为了作业的提交方便,不采用多个类进行数值专递
  4. 题目的数值可以是由机器提供,也可以用户自行设计
  5. 考虑到使用对象是小学生,所以界面设计上应越和谐越好,操作上也要容易上手
  6. 由于是针对小学生的题目,所以在除法不采用小数和分数,而是用整除取余的方法代替
  7. 小学并未涉及到负数,所以在减法上只出现大数减小数的情况
  8. 数值上也规定只出现1~100之间的数进行加、减、乘、除

代码实现:

  1 package View;
  2 
  3 import javax.swing.*;
  4 import java.util.*;
  5 import java.awt.*;
  6 import java.awt.event.*;
  7 
  8 public class Show extends JFrame implements ActionListener {
  9     // 出题部分
 10     JLabel jl1, jl2, jl3, jl4, jl5, jl6, jl7, jl8, jl9;
 11     JTextField jf1, jf2, jf3, jf4;
 12     JButton jb1, jb5;
 13     // 统计部分
 14     JButton jb;
 15     JTextField jl21, jl22, jl23, jl24, jl25, jl26, jl27, jl28, jl29, jl210,
 16             jl211, jl212, jl213, jl214, jl215;
 17 
 18     public Show() {
 19         setTitle("计算加减乘除四则运算");
 20         setDefaultCloseOperation(EXIT_ON_CLOSE);
 21         setSize(874, 364);
 22         setLocation(200, 200);
 23         Container con = getContentPane();
 24         JPanel pan = new JPanel();
 25 
 26         // 出题、计算部分
 27         jb1 = new JButton("出题");
 28         jb1.addActionListener(this);
 29         jl1 = new JLabel("?");
 30         jl2 = new JLabel("?");
 31         jl3 = new JLabel("答案:");
 32         jl4 = new JLabel("请输入答案:");
 33         jl5 = new JLabel("余数:");
 34         jl6 = new JLabel("?");
 35         jl7 = new JLabel("余数:");
 36         jl8 = new JLabel("对/错:");
 37         jl9 = new JLabel("?");
 38         jf1 = new JTextField(5);
 39         jf2 = new JTextField(5);
 40         jf3 = new JTextField(5);
 41         jf4 = new JTextField(5);
 42         jb5 = new JButton("正确答案");
 43         jb5.addActionListener(this);
 44         Box b1 = Box.createHorizontalBox();
 45         b1.add(jb1);
 46         b1.add(Box.createHorizontalStrut(20));
 47         b1.add(jf1);
 48         b1.add(Box.createHorizontalStrut(5));
 49         b1.add(jl2);
 50         b1.add(Box.createHorizontalStrut(5));
 51         b1.add(jf2);
 52         b1.add(Box.createHorizontalStrut(5));
 53         b1.add(jl4);
 54         b1.add(Box.createHorizontalStrut(5));
 55         b1.add(jf3);
 56         b1.add(Box.createHorizontalStrut(5));
 57         b1.add(jl5);
 58         b1.add(Box.createHorizontalStrut(5));
 59         b1.add(jf4);
 60         b1.add(Box.createHorizontalStrut(20));
 61         b1.add(jb5);
 62         b1.add(Box.createHorizontalStrut(5));
 63         b1.add(jl3);
 64         b1.add(Box.createHorizontalStrut(5));
 65         b1.add(jl6);
 66         b1.add(Box.createHorizontalStrut(5));
 67         b1.add(jl7);
 68         b1.add(Box.createHorizontalStrut(5));
 69         b1.add(jl1);
 70         b1.add(Box.createHorizontalStrut(5));
 71         b1.add(jl8);
 72         b1.add(Box.createHorizontalStrut(5));
 73         b1.add(jl9);
 74 
 75         // 统计部分
 76         jl21 = new JTextField("0");
 77         jl22 = new JTextField("0");
 78         jl23 = new JTextField("0");
 79         jl24 = new JTextField("0");
 80         jl25 = new JTextField("0");
 81         jl26 = new JTextField("0");
 82         jl27 = new JTextField("0");
 83         jl28 = new JTextField("0");
 84         jl29 = new JTextField("0");
 85         jl210 = new JTextField("0");
 86         jl211 = new JTextField("0");
 87         jl212 = new JTextField("0");
 88         jl213 = new JTextField("0");
 89         jl214 = new JTextField("0");
 90         jl215 = new JTextField("0");
 91         jl21.setEditable(false);
 92         jl22.setEditable(false);
 93         jl23.setEditable(false);
 94         jl24.setEditable(false);
 95         jl25.setEditable(false);
 96         jl26.setEditable(false);
 97         jl27.setEditable(false);
 98         jl28.setEditable(false);
 99         jl29.setEditable(false);
100         jl210.setEditable(false);
101         jl211.setEditable(false);
102         jl212.setEditable(false);
103         jl213.setEditable(false);
104         jl214.setEditable(false);
105         jl215.setEditable(false);
106 
107         Box b21 = Box.createHorizontalBox();
108         b21.add(new JLabel("加法题:"));
109         b21.add(jl21);
110         b21.add(Box.createHorizontalStrut(50));
111         b21.add(new JLabel("对:"));
112         b21.add(jl22);
113         b21.add(Box.createHorizontalStrut(50));
114         b21.add(new JLabel("错:"));
115         b21.add(jl23);
116 
117         Box b22 = Box.createHorizontalBox();
118         b22.add(new JLabel("减法题:"));
119         b22.add(jl24);
120         b22.add(Box.createHorizontalStrut(50));
121         b22.add(new JLabel("对:"));
122         b22.add(jl25);
123         b22.add(Box.createHorizontalStrut(50));
124         b22.add(new JLabel("错:"));
125         b22.add(jl26);
126 
127         Box b23 = Box.createHorizontalBox();
128         b23.add(new JLabel("乘法题:"));
129         b23.add(jl27);
130         b23.add(Box.createHorizontalStrut(50));
131         b23.add(new JLabel("对:"));
132         b23.add(jl28);
133         b23.add(Box.createHorizontalStrut(50));
134         b23.add(new JLabel("错:"));
135         b23.add(jl29);
136 
137         Box b24 = Box.createHorizontalBox();
138         b24.add(new JLabel("除法题:"));
139         b24.add(jl210);
140         b24.add(Box.createHorizontalStrut(50));
141         b24.add(new JLabel("对:"));
142         b24.add(jl211);
143         b24.add(Box.createHorizontalStrut(50));
144         b24.add(new JLabel("错:"));
145         b24.add(jl212);
146 
147         Box b25 = Box.createHorizontalBox();
148         b25.add(new JLabel("总 计:"));
149         b25.add(jl213);
150         b25.add(Box.createHorizontalStrut(50));
151         b25.add(new JLabel("对:"));
152         b25.add(jl214);
153         b25.add(Box.createHorizontalStrut(50));
154         b25.add(new JLabel("错:"));
155         b25.add(jl215);
156 
157         Box b2 = Box.createVerticalBox();
158         b2.add(b21);
159         b2.add(b22);
160         b2.add(b23);
161         b2.add(b24);
162         b2.add(b25);
163 
164         Box b3 = Box.createVerticalBox();
165         b3.add(Box.createVerticalStrut(50));
166         b3.add(b1);
167         b3.add(Box.createVerticalStrut(25));
168         b3.add(b2);
169         b3.add(Box.createVerticalStrut(25));
170         jb = new JButton("清除记录");
171         jb.addActionListener(this);
172         b3.add(jb);
173         b3.add(Box.createVerticalStrut(50));
174 
175         pan.add(b3);
176         con.add(pan);
177         setVisible(true);
178         // pack();
179     }
180 
181     public static void main(String args[]) {
182         Show main = new Show();
183     }
184 
185     @Override
186     public void actionPerformed(ActionEvent e) {
187         if (e.getSource() == jb) {
188             jf1.setText("");
189             jf2.setText("");
190             jf3.setText("");
191             jf4.setText("");
192             jl1.setText("?");
193             jl2.setText("?");
194             jl6.setText("?");
195             jl9.setText("?");
196             jl21.setText("0");
197             jl22.setText("0");
198             jl23.setText("0");
199             jl24.setText("0");
200             jl25.setText("0");
201             jl26.setText("0");
202             jl27.setText("0");
203             jl28.setText("0");
204             jl29.setText("0");
205             jl210.setText("0");
206             jl211.setText("0");
207             jl212.setText("0");
208             jl213.setText("0");
209             jl214.setText("0");
210             jl215.setText("0");
211         }
212         if (e.getSource() == jb1) {
213             jf3.setText("");
214             jf4.setText("");
215             jl1.setText("?");
216             jl2.setText("?");
217             jl6.setText("?");
218             jl9.setText("?");
219             Random rand = new Random();
220             int i = rand.nextInt(100);
221             int j = rand.nextInt(100);
222             i = i + 1;
223             j = j + 1;
224             if (i < j) {
225                 String s = Integer.toString(i);
226                 String y = Integer.toString(j);
227 
228                 jf1.setText(y);
229                 jf2.setText(s);
230             } else {
231                 String s = Integer.toString(i);
232                 String y = Integer.toString(j);
233                 jf1.setText(s);
234                 jf2.setText(y);
235             }
236             int f = rand.nextInt();
237             f = rand.nextInt(4);
238             if (f == 0) {
239                 jl2.setText("+");
240             } else if (f == 1) {
241                 jl2.setText("-");
242             } else if (f == 2) {
243                 jl2.setText("*");
244             } else if (f == 3) {
245                 jl2.setText("/");
246             }
247         }
248         if (e.getSource() == jb5) {
249             int b, c, d, f, h, g, k, l, m, n, o, p;
250             int r, t, u;
251             r = Integer.parseInt(jl213.getText()) + 1;
252             jl213.setText(Integer.toString(r));
253             jl1.setText("?");
254             int x, y, answer = 0;
255             String z, i, j, s = null;
256             x = Integer.parseInt(jf1.getText());
257             y = Integer.parseInt(jf2.getText());
258             z = jl2.getText();
259             i = jf3.getText();
260             j = jf4.getText();
261             if (z == "+") {
262                 b = Integer.parseInt(jl21.getText()) + 1;
263                 jl21.setText(Integer.toString(b));
264                 answer = x + y;
265             } else if (z == "-") {
266                 f = Integer.parseInt(jl24.getText()) + 1;
267                 jl24.setText(Integer.toString(f));
268                 answer = x - y;
269             } else if (z == "*") {
270                 k = Integer.parseInt(jl27.getText()) + 1;
271                 jl27.setText(Integer.toString(k));
272                 answer = x * y;
273             } else if (z == "/") {
274                 n = Integer.parseInt(jl210.getText()) + 1;
275                 jl210.setText(Integer.toString(n));
276                 answer = x / y;
277                 jl1.setText("0");
278                 if (x % y != 0) {
279                     s = Integer.toString(x % y);
280                     jl1.setText(s);
281                 }
282             }
283             String a = Integer.toString(answer);
284             jl6.setText(a);
285             if (i.equals(a)) {
286                 if (s == null) {
287                     jl9.setText("对");
288                     t = Integer.parseInt(jl214.getText()) + 1;
289                     jl214.setText(Integer.toString(t));
290                     if (z == "+") {
291                         c = Integer.parseInt(jl22.getText()) + 1;
292                         jl22.setText(Integer.toString(c));
293                     } else if (z == "-") {
294                         h = Integer.parseInt(jl25.getText()) + 1;
295                         jl25.setText(Integer.toString(h));
296                     } else if (z == "*") {
297                         l = Integer.parseInt(jl28.getText()) + 1;
298                         jl28.setText(Integer.toString(l));
299                     } else if (z == "/") {
300                         o = Integer.parseInt(jl211.getText()) + 1;
301                         jl211.setText(Integer.toString(o));
302                     }
303                 } else if (j.equals(s)) {
304                     jl9.setText("对");
305                     t = Integer.parseInt(jl214.getText()) + 1;
306                     jl214.setText(Integer.toString(t));
307                     if (z == "+") {
308                         c = Integer.parseInt(jl22.getText()) + 1;
309                         jl22.setText(Integer.toString(c));
310                     } else if (z == "-") {
311                         h = Integer.parseInt(jl25.getText()) + 1;
312                         jl25.setText(Integer.toString(h));
313                     } else if (z == "*") {
314                         l = Integer.parseInt(jl28.getText()) + 1;
315                         jl28.setText(Integer.toString(l));
316                     } else if (z == "/") {
317                         o = Integer.parseInt(jl211.getText()) + 1;
318                         jl211.setText(Integer.toString(o));
319                     }
320                 } else {
321                     jl9.setText("错");
322                     u = Integer.parseInt(jl215.getText()) + 1;
323                     jl215.setText(Integer.toString(u));
324                     if (z == "+") {
325                         d = Integer.parseInt(jl23.getText()) + 1;
326                         jl23.setText(Integer.toString(d));
327                     } else if (z == "-") {
328                         g = Integer.parseInt(jl26.getText()) + 1;
329                         jl26.setText(Integer.toString(g));
330                     } else if (z == "*") {
331                         m = Integer.parseInt(jl29.getText()) + 1;
332                         jl29.setText(Integer.toString(m));
333                     } else if (z == "/") {
334                         p = Integer.parseInt(jl212.getText()) + 1;
335                         jl212.setText(Integer.toString(p));
336                     }
337                 }
338             } else {
339                 jl9.setText("错");
340                 u = Integer.parseInt(jl215.getText()) + 1;
341                 jl215.setText(Integer.toString(u));
342                 if (z == "+") {
343                     d = Integer.parseInt(jl23.getText()) + 1;
344                     jl23.setText(Integer.toString(d));
345                 } else if (z == "-") {
346                     g = Integer.parseInt(jl26.getText()) + 1;
347                     jl26.setText(Integer.toString(g));
348                 } else if (z == "*") {
349                     m = Integer.parseInt(jl29.getText()) + 1;
350                     jl29.setText(Integer.toString(m));
351                 } else if (z == "/") {
352                     p = Integer.parseInt(jl212.getText()) + 1;
353                     jl212.setText(Integer.toString(p));
354                 }
355             }
356         }
357     }
358 }

测试:

第一步:打开界面

第二步:点击“出题”按钮,出现题目

第三步:输入答案,然后点击“正确答案”按钮进行判断对错并统计

第四步:点击“清除记录”按钮,清除本次测试记录,方便进行下一轮测试

事后分析和总结:

PSP耗时:

项目 时间 时间(%)
需求分析 20分钟 6
代码设计 35分钟 12
代码编写 180分钟 57
测试 60分钟 19
分析总结 20分钟 6

分析:

本次编程出现过的错误及盲区:

  1. 变量的命名过于繁乱,导致不易发现的错误
  2. 思路不够明确,总是想一点做一点,延长整体的编程过程
  3. 对题目的理解不仔细,走错编程方向
  4. 用户需求分析不明确,小学生的除法水平在哪个位置?

总结:

    • 通过这次作业,深刻体会到了编写程序前对程序的构思和设计的重要性,通过构思和设计可以为我节省很多不必要的时间开销;
    • 在对整个程序的构思的时候花费了较多的时间,这可能是还没有太多编程经验导致的吧,希望以后在这方面能够掌握好时间开销;
    • 这一次的编程过程又是一次边写边改的过程,相信每一次的出错都是一次进步;
    • 没有最好的程序只有更好的程序,这是每次编程都能体会到的感受。
原文地址:https://www.cnblogs.com/wjfsrc/p/4418299.html