结对编程实现四则运算

   结对编程技术是指两位程序员坐在同一工作台前开发软件。与两位程序员各自独立工作相比,结对编程能编写出质量更高的代码。

按照结对编程的原则,我和小组另一个成员进行了两人进行了结对编程,我们开发的结对项目的是小学的四则运算,具体就是随机生成

30道题,供学生答题并返回正误。经过讨论,我们将采用Java的图形化界面完成,分工为两部分,一人负责四则运算的产生,一个界面

以及运算后的界面。

这是我们的运算原理代码:

 1 package com.cj;
 2 
 3 import java.util.Random;
 4 
 5 public class Out {
 6     private int a, b;
 7     private int i;
 8     private String operator[]={"+","-","×","÷"};
 9     
10     public String getYun(){
11       while(true){
12         a=new Random().nextInt(100);
13         b=new Random().nextInt(100);  //产生随机数
14         i=new Random().nextInt(4);
15         
16         if(i==1&&a<b){         //不能为负数
17             continue;
18         }
19         
20         if(i==3){             //不能被0除
21             if(b==0){
22                 continue;
23             }
24             if(a%b!=0){      //a一定被b除
25                 continue;
26             }
27         }
28         break;       
29       }
30       return new String(a+operator[i]+b+"=");
31     }
32     
33     
34     public  boolean panduan(String s){    //判断答案是否正确
35         int i,result = 0;
36         try{
37             i=Integer.valueOf(s).intValue();
38         }catch(Exception e){
39             return false;
40         }
41         switch(this.operator()){
42            case "+":result=this.getA()+this.getB();break;
43            case "-":result=this.getA()-this.getB();break;
44            case "×":result=this.getA()*this.getB();break;
45            case "÷":result=this.getA()/this.getB();break;
46         }
47         if(result==i){
48             return true;
49         }return false;
50         
51     }
52     
53     public int correctResult(){     //返回正确的答案
54         int result=0;
55        switch(this.operator()){
56         case "+":result=this.getA()+this.getB();break;
57         case "-":result=this.getA()-this.getB();break;
58         case "×":result=this.getA()*this.getB();break;
59         case "÷":result=this.getA()/this.getB();break;
60      }
61        return result;
62     }
63     
64     
65     public String operator(){        //返回运算符
66         return operator[this.i];
67     }
68     
69     
70     public int getA() {  //返回运算数
71         return a;
72     }
73 
74 
75     public int getB() { //返回运算数
76         return b;
77     }
78     
79 }
  1 package com.cj.jg;
  2 
  3 import java.awt.BorderLayout;
  4 import java.awt.Color;
  5 import java.awt.Container;
  6 import java.awt.FlowLayout;
  7 import java.awt.GridLayout;
  8 import java.awt.event.ActionEvent;
  9 import java.awt.event.ActionListener;
 10 
 11 import javax.swing.DefaultListModel;
 12 import javax.swing.JButton;
 13 import javax.swing.JFrame;
 14 import javax.swing.JLabel;
 15 import javax.swing.JList;
 16 import javax.swing.JPanel;
 17 import javax.swing.JScrollPane;
 18 import javax.swing.JTextField;   //引入相关的包
 19 
 20 public class Ui {
 21     
 22     
 23     
 24     public static JScrollPane jls;
 25     public static int correct=0;
 26     public static int number=0;
 27     public static int total=0;
 28     public static boolean flag=false;
 29     public static JTextField count;
 30         public static JButton btn_ok;
 31         public static JButton btn_xuan;
 32         public static JLabel operate;
 33         public static JTextField result;
 34         public static JLabel info;
 35         public static JList infoList;         //声明各种控件
 36     public static DefaultListModel dlm = new DefaultListModel();
 37     public static Out out=new Out();
 38      
 39     public static boolean panduan(String s){   //判断用户输入的题数是否是整数且在1-100的范围内
 40         int i;
 41         try
 42         {
 43             i=Integer.valueOf(s).intValue();
 44         }catch(Exception e){
 45             return false;
 46         }
 47         if(i<=0||i>100)
 48         {
 49               return false;
 50         }        
 51         return true;
 52     } 
 53     
 54     public static void main(String[] args) {
 55         // TODO Auto-generated method stub
 56          JFrame frame=new JFrame("四则运算");
 57          Container contentPane=frame.getContentPane();
 58          
 59          contentPane.setLayout(new GridLayout(2,1));  //界面布局
 60          
 61          JPanel panel_top=new JPanel();
 62          panel_top.setLayout(new BorderLayout());
 63          
 64          
 65          
 66          JPanel panel_one=new JPanel(new FlowLayout(FlowLayout.LEFT));  
 67          count=new JTextField(5);
 68          btn_xuan=new JButton("确定");
 69          btn_xuan.addActionListener(new ActionListener(){
 70 
 71             @Override
 72             public void actionPerformed(ActionEvent e) {
 73                 // TODO Auto-generated method stub
 74             
 75                 
 76                 if(Ui.panduan(Ui.count.getText())){
 77                     Ui.out=new Out();
 78                     Ui.correct=0;
 79                     Ui.total=Ui.number=Integer.valueOf(Ui.count.getText()).intValue();
 80                     Ui.flag=true;
 81                     Ui.btn_ok.setEnabled(true);
 82                     Ui.result.setEnabled(true);
 83                     Ui.btn_xuan.setText("重新生成");
 84                     
 85                     Ui.operate.setText(Ui.out.getYun());
 86                     Ui.info.setText("你还剩余"+Ui.number+"道题,你已经答对"+Ui.correct+"道题"+",你目前的正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total)));
 87                     Ui.dlm.clear();
 88                     Ui.infoList.setModel(Ui.dlm);
 89                 }
 90             }
 91                    
 92          });   //确定题数按钮事件的处理
 93          panel_one.add(new JLabel("产生题目数:"));
 94          panel_one.add(count);
 95          panel_one.add(btn_xuan);
 96          panel_one.add(new JLabel("1-100"));
 97          
 98          
 99          
100          JPanel panel_second=new JPanel(new FlowLayout(FlowLayout.LEFT));
101          operate=new JLabel("0+0=");  
102          result=new JTextField(5);
103          result.setEnabled(false);
104          btn_ok=new JButton("提交");
105          btn_ok.addActionListener(new ActionListener(){
106 
107             @Override
108             public void actionPerformed(ActionEvent e) {
109                 // TODO Auto-generated method stub
110                 StringBuffer s=new StringBuffer();
111                 if(Ui.out.panduan(Ui.result.getText())){
112                     Ui.correct++;
113                     Ui.number--;
114                     
115                     s.append(Ui.out.getA()).append(Ui.out.operator()).append(Ui.out.getB()+"=").append(Ui.result.getText()).append("	").append("   √    ");
116                     
117                 }else{
118                     Ui.number--;
119                     s.append(Ui.out.getA()).append(Ui.out.operator()).append(Ui.out.getB()+"=").append(Ui.result.getText()).append("	").append("   ×    ").append(Ui.out.correctResult());
120                     
121                 }
122                 Ui.info.setText("你还剩余"+Ui.number+"道题,你已经答对"+Ui.correct+"道题"+",你目前的正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total)));
123                 if(Ui.number==0){
124                      Ui.btn_ok.setEnabled(false);
125                      Ui.info.setForeground(Color.red);
126                      Ui.info.setText("恭喜你!你总共完成"+Ui.total+"道题"+",你的答题正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total)));
127                      
128                 }
129                 
130                 Ui.dlm.addElement(s);
131                 Ui.infoList.setModel(Ui.dlm);
132                 
133                 Ui.out=new Out();
134                 Ui.operate.setText(Ui.out.getYun());
135             
136             }
137              
138          }); //提交结果按钮事件处理
139          Ui.operate.setForeground(Color.BLUE);
140          
141          btn_ok.setEnabled(false);
142          panel_second.add(new JLabel("         运算:"));       
143          panel_second.add(operate);
144          panel_second.add(result);
145          panel_second.add(btn_ok);
146          
147          
148          
149          
150          JPanel panel_third=new JPanel(new FlowLayout(FlowLayout.LEFT));
151          info=new JLabel("");
152          panel_third.add(new JLabel("提示:"));
153          panel_third.add(info);
154          
155         
156          infoList=new JList();
157          jls=new JScrollPane(infoList);
158          
159          panel_top.add(panel_one,BorderLayout.NORTH);
160          panel_top.add(panel_second,BorderLayout.CENTER);
161          panel_top.add(panel_third,BorderLayout.SOUTH);
162          
163          contentPane.add(panel_top);
164          contentPane.add(jls);
165          frame.setSize(450, 500);
166          frame.setVisible(true);
167     }
168 
169 
170 }

  运行出来后的效果图:

 

原文地址:https://www.cnblogs.com/NMSLWSND/p/5283966.html