上次说到了结对编程这个话题,但是匆匆几句,写的不尽人意,这一次,我将为大家带来详细的结对编程体验感受~~~
首先,介绍一下我的结对编程伙伴——陈小宇,他是一个名副其实的编程高手。以下是他的博客地址:http://www.cnblogs.com/cxyc
附上一张我们在完成任务的过程中的留影~~
在课题的选择中,我们选择了四则运算这个课题,相对于大三的我们来说还是比较简单的。但是一旦开始做起来,才发现问题多多。对于变量的命名、任务分配、进度调整....等等困难接踵而至,让我们措手不及。但最后我们都通过及时的交流克服了这些困难。
下面是我们的作品源码:
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 }
UI.class
package com.cj.jg; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; //引入相关的包 public class Ui { public static JScrollPane jls; public static int correct=0; public static int number=0; public static int total=0; public static boolean flag=false; public static JTextField count; public static JButton btn_ok; public static JButton btn_xuan; public static JLabel operate; public static JTextField result; public static JLabel info; public static JList infoList; //声明各种控件 public static DefaultListModel dlm = new DefaultListModel(); public static Out out=new Out(); public static boolean panduan(String s){ //判断用户输入的题数是否是整数且在1-100的范围内 int i; try { i=Integer.valueOf(s).intValue(); }catch(Exception e){ return false; } if(i<=0||i>100) { return false; } return true; } public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame=new JFrame("四则运算"); Container contentPane=frame.getContentPane(); contentPane.setLayout(new GridLayout(2,1)); //界面布局 JPanel panel_top=new JPanel(); panel_top.setLayout(new BorderLayout()); JPanel panel_one=new JPanel(new FlowLayout(FlowLayout.LEFT)); count=new JTextField(5); btn_xuan=new JButton("确定"); btn_xuan.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(Ui.panduan(Ui.count.getText())){ Ui.out=new Out(); Ui.correct=0; Ui.total=Ui.number=Integer.valueOf(Ui.count.getText()).intValue(); Ui.flag=true; Ui.btn_ok.setEnabled(true); Ui.result.setEnabled(true); Ui.btn_xuan.setText("重新生成"); Ui.operate.setText(Ui.out.getYun()); Ui.info.setText("你还剩余"+Ui.number+"道题,你已经答对"+Ui.correct+"道题"+",你目前的正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total))); Ui.dlm.clear(); Ui.infoList.setModel(Ui.dlm); } } }); //确定题数按钮事件的处理 panel_one.add(new JLabel("产生题目数:")); panel_one.add(count); panel_one.add(btn_xuan); panel_one.add(new JLabel("1-100")); JPanel panel_second=new JPanel(new FlowLayout(FlowLayout.LEFT)); operate=new JLabel("0+0="); result=new JTextField(5); result.setEnabled(false); btn_ok=new JButton("提交"); btn_ok.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub StringBuffer s=new StringBuffer(); if(Ui.out.panduan(Ui.result.getText())){ Ui.correct++; Ui.number--; s.append(Ui.out.getA()).append(Ui.out.operator()).append(Ui.out.getB()+"=").append(Ui.result.getText()).append(" ").append(" √ "); }else{ Ui.number--; s.append(Ui.out.getA()).append(Ui.out.operator()).append(Ui.out.getB()+"=").append(Ui.result.getText()).append(" ").append(" × ").append(Ui.out.correctResult()); } Ui.info.setText("你还剩余"+Ui.number+"道题,你已经答对"+Ui.correct+"道题"+",你目前的正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total))); if(Ui.number==0){ Ui.btn_ok.setEnabled(false); Ui.info.setForeground(Color.red); Ui.info.setText("恭喜你!你总共完成"+Ui.total+"道题"+",你的答题正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total))); } Ui.dlm.addElement(s); Ui.infoList.setModel(Ui.dlm); Ui.out=new Out(); Ui.operate.setText(Ui.out.getYun()); } }); //提交结果按钮事件处理 Ui.operate.setForeground(Color.BLUE); btn_ok.setEnabled(false); panel_second.add(new JLabel(" 运算:")); panel_second.add(operate); panel_second.add(result); panel_second.add(btn_ok); JPanel panel_third=new JPanel(new FlowLayout(FlowLayout.LEFT)); info=new JLabel(""); panel_third.add(new JLabel("提示:")); panel_third.add(info); infoList=new JList(); jls=new JScrollPane(infoList); panel_top.add(panel_one,BorderLayout.NORTH); panel_top.add(panel_second,BorderLayout.CENTER); panel_top.add(panel_third,BorderLayout.SOUTH); contentPane.add(panel_top); contentPane.add(jls); frame.setSize(450, 500); frame.setVisible(true); } }
下面是作品截图展示:
以上就是我们这次结对编程的全部内容了...合作愉快@陈晓宇