PSP流程下的四则运算

第一部分:要求

  • 自动生成题目
  • 用户可以输入答案
  • 若用户输入答案正确,则提示正确;若答案错误,则提示错误

第二部分:估计实现所需时间

 

PSP

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

300

240

· Estimate

估计这个任务需要多少时间

300

600

Development

开发

100

200

· Analysis

需求分析 (包括学习新技术)

60

60

· Design Spec

生成设计文档

0

0

· Design Review

设计复审

30

0

· Coding Standard

代码规范

0

0

· Design

具体设计

30

45

· Coding

具体编码

40

60

· Code Review

代码复审

10

10

· Test

测试(自我测试,修改代码,提交修改)

180

180

Reporting

报告

200

250

Test Report

测试报告

0

0

·workload

计算工作量

20

20

·correction

并提出过程改进计划

0

0

第三部分:实现

  1 package zz;
  2 
  3 import java.awt.*;
  4 import javax.swing.*;
  5 import java.awt.event.*;
  6 import java.util.*;
  7 
  8 public class test11{
  9     public static void main (String args[]) {
 10         new WinJFrame();    
 11     } 
 12 }
 13 
 14 class WinJFrame extends JFrame{
 15     public WinJFrame(){
 16         setBounds(400,200,600,400);
 17         setTitle("四则运算");
 18         setLayout(null);
 19         Container con = getContentPane();
 20         con.setBackground(Color.pink);
 21         
 22         init();
 23         
 24         setVisible(true);
 25         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 26     }
 27     
 28     public int getPlus(String s) {
 29         String regex = "[^0123456789.]+";
 30         s=s.replaceAll(regex, "#");
 31         StringTokenizer fenxi = new StringTokenizer(s,"#");
 32         int sum = 0;
 33         while(fenxi.hasMoreTokens()) {
 34             String item = fenxi.nextToken();
 35             int h =Integer.parseInt(item);
 36             sum = sum + h;
 37         }
 38         return sum;
 39     }
 40     
 41     public int getMinus(String s) {
 42         String regex = "[^0123456789.]+";
 43         s=s.replaceAll(regex, "#");
 44         StringTokenizer fenxi = new StringTokenizer(s,"#");
 45         int sum = 0;
 46         int ss[];
 47         ss=new int[2];
 48         for(int i=0;fenxi.hasMoreTokens();i++)
 49         {
 50             String item = fenxi.nextToken();
 51             ss[i]=Integer.parseInt(item);
 52         }
 53         sum=ss[0]-ss[1];
 54     
 55         return sum;
 56     }
 57     
 58     public int getMultiply(String s) {
 59         String regex = "[^0123456789.]+";
 60         s=s.replaceAll(regex, "#");
 61         StringTokenizer fenxi = new StringTokenizer(s,"#");
 62         int sum = 0;
 63         int ss[];
 64         ss=new int[2];
 65         for(int i=0;fenxi.hasMoreTokens();i++)
 66         {
 67             String item = fenxi.nextToken();
 68             ss[i]=Integer.parseInt(item);
 69         }
 70         sum=ss[0]*ss[1];
 71     
 72         return sum;
 73     }
 74     
 75     public int getDivision(String s) {
 76         String regex = "[^0123456789.]+";
 77         s=s.replaceAll(regex, "#");
 78         StringTokenizer fenxi = new StringTokenizer(s,"#");
 79         int sum = 0;
 80         int ss[];
 81         ss=new int[2];
 82         for(int i=0;fenxi.hasMoreTokens();i++)
 83         {
 84             String item = fenxi.nextToken();
 85             ss[i]=Integer.parseInt(item);
 86         }
 87         sum=ss[0]/ss[1];
 88     
 89         return sum;
 90     }
 91     void init() {
 92         JButton b[] = new JButton[12];
 93         b[0]=new JButton();
 94         b[1]=new JButton();
 95         b[2]=new JButton();
 96         b[3]=new JButton();
 97         b[4]=new JButton();
 98         b[5]=new JButton();
 99         b[6]=new JButton();
100         b[7]=new JButton();
101         b[8]=new JButton();
102         b[9]=new JButton();
103         b[10]=new JButton();
104         b[11]=new JButton();
105         
106         add(b[0]);
107         add(b[1]);
108         add(b[2]);
109         add(b[3]);
110         add(b[4]);
111         add(b[5]);
112         add(b[6]);
113         add(b[7]);
114         add(b[8]);
115         add(b[9]);
116         add(b[10]);
117         add(b[11]);
118         
119         b[0].setBounds(20,20,100,30);
120         b[0].setText("加法");
121         b[1].setBounds(460,20,100,30);
122         b[1].setText("重置");
123         b[2].setBounds(20,60,100,30);
124         b[2].setText("确认答案");
125         
126         b[3].setBounds(20,100,100,30);
127         b[3].setText("减法");
128         b[4].setBounds(460,100,100,30);
129         b[4].setText("重置");
130         b[5].setBounds(20,140,100,30);
131         b[5].setText("确认答案");
132         
133         b[6].setBounds(20,180,100,30);
134         b[6].setText("乘法");
135         b[7].setBounds(460,180,100,30);
136         b[7].setText("重置");
137         b[8].setBounds(20,220,100,30);
138         b[8].setText("确认答案");
139         
140         b[9].setBounds(20,260,100,30);
141         b[9].setText("除法");
142         b[10].setBounds(460,260,100,30);
143         b[10].setText("重置");
144         b[11].setBounds(20,300,100,30);
145         b[11].setText("确认答案");
146         
147         JTextArea ja0 = new JTextArea();
148         JTextArea ja1 = new JTextArea();
149         JTextArea ja2 = new JTextArea();
150         JTextArea ja3 = new JTextArea();
151         
152         ja0.setBounds(120, 20, 340, 30);
153         ja0.setFont(new Font("黑体",Font.BOLD,30));
154         add(ja0);
155         
156         ja1.setBounds(120, 100, 340, 30);
157         ja1.setFont(new Font("黑体",Font.BOLD,30));
158         add(ja1);
159         
160         ja2.setBounds(120, 180, 340, 30);
161         ja2.setFont(new Font("黑体",Font.BOLD,30));
162         add(ja2);
163         
164         ja3.setBounds(120, 260, 340, 30);
165         ja3.setFont(new Font("黑体",Font.BOLD,30));
166         add(ja3);
167         
168         JTextField jt0 = new JTextField();
169         JTextField jt1 = new JTextField();
170         JTextField jt2 = new JTextField();
171         JTextField jt3 = new JTextField();
172         
173         jt0.setBounds(120, 60, 340, 30);
174         jt0.setFont(new Font("黑体",Font.BOLD,30));
175         add(jt0);
176         
177         jt1.setBounds(120, 140, 340, 30);
178         jt1.setFont(new Font("黑体",Font.BOLD,30));
179         add(jt1);
180         
181         jt2.setBounds(120, 220, 340, 30);
182         jt2.setFont(new Font("黑体",Font.BOLD,30));
183         add(jt2);
184         
185         jt3.setBounds(120, 300, 340, 30);
186         jt3.setFont(new Font("黑体",Font.BOLD,30));
187         add(jt3);
188         
189         for(int j=0;j<2;j++) {
190             b[j].addActionListener(new ActionListener() {
191                 public void actionPerformed(ActionEvent e) {
192                     int number1,number2;
193                     number1=(int)Math.ceil(Math.random()*100);
194                     number2=(int)Math.ceil(Math.random()*100);
195                     ja0.setText(number1+"+"+number2);
196                 }
197             });
198         }
199         
200         for(int j=3;j<5;j++) {
201             b[j].addActionListener(new ActionListener() {
202                 public void actionPerformed(ActionEvent e) {
203                     int number1,number2;
204                     number1=(int)Math.ceil(Math.random()*100);
205                     number2=(int)Math.ceil(Math.random()*100);
206                     ja1.setText(number1+"-"+number2);
207                 }
208             });
209         }
210         
211         for(int j=6;j<8;j++) {
212             b[j].addActionListener(new ActionListener() {
213                 public void actionPerformed(ActionEvent e) {
214                     int number1,number2;
215                     number1=(int)Math.ceil(Math.random()*100);
216                     number2=(int)Math.ceil(Math.random()*100);
217                     ja2.setText(number1+"*"+number2);
218                 }
219             });
220         }
221         
222         for(int j=9;j<11;j++) {
223             b[j].addActionListener(new ActionListener() {
224                 public void actionPerformed(ActionEvent e) {
225                     int number1,number2;
226                     number1=(int)Math.ceil(Math.random()*100);
227                     number2=(int)Math.ceil(Math.random()*100);
228                     ja3.setText(number1+"/"+number2);
229                 }
230             });
231         }
232         
233         b[2].addActionListener(new ActionListener() {
234             public void actionPerformed(ActionEvent e) {
235                 if(getPlus(ja0.getText())==Integer.parseInt(jt0.getText())){
236                     JOptionPane.showMessageDialog(jt0,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE);    
237                 }
238                 else{
239                     JOptionPane.showMessageDialog(jt0,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE);
240                 }
241             }
242         });
243         
244         b[5].addActionListener(new ActionListener() {
245             public void actionPerformed(ActionEvent e) {
246                 if(getMinus(ja1.getText())==Integer.parseInt(jt1.getText())){
247                     JOptionPane.showMessageDialog(jt1,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE);    
248                 }
249                 else{
250                     JOptionPane.showMessageDialog(jt1,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE);
251                 }
252             }
253         });
254         
255         b[8].addActionListener(new ActionListener() {
256             public void actionPerformed(ActionEvent e) {
257                 if(getMultiply(ja2.getText())==Integer.parseInt(jt2.getText())){
258                     JOptionPane.showMessageDialog(jt2,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE);    
259                 }
260                 else{
261                     JOptionPane.showMessageDialog(jt2,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE);
262                 }
263             }
264         });
265         
266         b[11].addActionListener(new ActionListener() {
267             public void actionPerformed(ActionEvent e) {
268                 if(getDivision(ja3.getText())==Integer.parseInt(jt3.getText())){
269                     JOptionPane.showMessageDialog(jt3,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE);    
270                 }
271                 else{
272                     JOptionPane.showMessageDialog(jt3,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE);
273                 }
274             }
275         });
276         }
277     }

 

posted @ 2021-09-28 12:41  zhENie  阅读(45)  评论(0)    收藏  举报