结对编程5.1
import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Random; import java.awt.Font; public class calculator { private JFrame frame; private JTextField []textField= new JTextField[5]; JLabel []label = new JLabel[5]; int []r1=new int[5]; int []r2=new int[5]; String str3=""; int Sum; int n=1; int[]sum=new int[5]; String []r3=new String[5]; String []str=new String[5]; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { calculator window = new calculator(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public calculator() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setTitle("\u5C0F\u5B66\u56DB\u5219\u8FD0\u7B97"); frame.setBounds(100, 100, 450, 362); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); label[0] = new JLabel(); label[0].setFont(new Font("宋体", Font.PLAIN, 27)); label[0].setBounds(10, 50, 99, 29); frame.getContentPane().add(label[0]); label[1] = new JLabel(); label[1].setFont(new Font("宋体", Font.PLAIN, 27)); label[1].setBounds(10, 89, 99, 29); frame.getContentPane().add(label[1]); label[2] = new JLabel(); label[2].setFont(new Font("宋体", Font.PLAIN, 27)); label[2].setBounds(10, 128, 99, 29); frame.getContentPane().add(label[2]); label[3] = new JLabel(); label[3].setFont(new Font("宋体", Font.PLAIN, 27)); label[3].setBounds(10, 167, 99, 29); frame.getContentPane().add(label[3]); label[4] = new JLabel(); label[4].setFont(new Font("宋体", Font.PLAIN, 27)); label[4].setBounds(10, 206, 99, 29); frame.getContentPane().add(label[4]); textField[0] = new JTextField(); textField[0].setFont(new Font("宋体", Font.PLAIN, 27)); textField[0].setBounds(128, 50, 66, 29); frame.getContentPane().add(textField[0]); textField[0].setColumns(10); textField[1] = new JTextField(); textField[1].setFont(new Font("宋体", Font.PLAIN, 27)); textField[1].setColumns(10); textField[1].setBounds(128, 93, 66, 29); frame.getContentPane().add(textField[1]); textField[2] = new JTextField(); textField[2].setFont(new Font("宋体", Font.PLAIN, 27)); textField[2].setColumns(10); textField[2].setBounds(128, 132, 66, 29); frame.getContentPane().add(textField[2]); textField[3] = new JTextField(); textField[3].setFont(new Font("宋体", Font.PLAIN, 27)); textField[3].setColumns(10); textField[3].setBounds(128, 171, 66, 29); frame.getContentPane().add(textField[3]); textField[4] = new JTextField(); textField[4].setFont(new Font("宋体", Font.PLAIN, 27)); textField[4].setColumns(10); textField[4].setBounds(128, 210, 66, 29); frame.getContentPane().add(textField[4]); JButton button = new JButton("\u51FA\u9898"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Stamp st=new Stamp(); sum=st.output(r1, r2, r3);//我想测一测 //将初始化的数组r1和r2的{1.2.3.4.5}传入 //我的output方法里 //得出的结果会不会是:和传入的值一样 for(int i=0;i<5;i++) { textField[i].setText(null); Sum=0; if(!r3[i].equals("!")) label[i].setText(r1[i]+r3[i]+r2[i]+"="); else//if(r3[i].equals("!")) { label[i].setText(r2[i]+r3[i]+"=");} } } }); button.setBounds(16, 273, 93, 23); frame.getContentPane().add(button); // while(){ JButton button_1 = new JButton("\u786E\u5B9A"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { int q=1;int []a=new int[5];int l=0; for(int i=0;i<5;i++) {a[i]=-1; str[i]=textField[i].getText(); char[] ch=str[i].toCharArray(); for(int j=str[i].length()-1;j>-1;j--) { if(ch[j]<'0'||ch[j]>'9'){ a[l]=i; l++; q=0; break; } } }if(a[1]!=-1){ JOptionPane w=new JOptionPane(); w.showMessageDialog(w,"输入有非法字符!", "温馨提示", 0); for(int i=0;i<l;i++){ textField[a[i]].setText(null); } } if(q==1){ Calc Ca=new Calc(); int k=Ca.cc(r1, r2, r3, sum, str); str3="答对"+k/20+"题"+"\n"+"共得"+k+"分"; JOptionPane sm=new JOptionPane(); sm.showMessageDialog(sm, str3, "温馨提示", 1); Jf w=new Jf(); n=w.Jfw(); }} }); //frame.dispose(); button_1.setBounds(130, 273, 93, 23); frame.getContentPane().add(button_1); } public void close(){ frame.dispose(); } } public class Calc { public int cc(int r1[], int r2[], String r3[],int sum[],String str[]){ //Stamp st=new Stamp(); //st.output(r1, r2, r3,sum); int fenshu=0; for(int i=0;i<5;i++) {int n=Integer.valueOf(str[i]); System.out.println(str[i]+" "+sum[i]); if(n==sum[i]) { fenshu+=20; } //System.out.println(sum[i]); } System.out.print(fenshu); return fenshu; } } import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Jf extends JFrame{ JButton b1 = new JButton("是"); JButton b2 = new JButton("否"); JLabel label = new JLabel("是否继续:(退出点击关闭)"); int k=1; int n=1; public int Jfw(){ this.setTitle("select"); this.setResizable(false); this.setBounds(500, 350, 300, 100); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); FlowLayout flow=new FlowLayout(); this.getContentPane().setLayout(flow); add(label); add(b1);//add(b2); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { n=1; close(); } }); /* b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { n=0; close(); } });*/ return n; } public void close(){ this.dispose(); } } import java.util.Random; public class Stamp { static Random r = new Random(); int y; int []sum=new int[5]; public int[] output(int r1[],int r2[],String r3[]){ for(int i=0;i<5;i++){ sum[i]=0; r1[i] = r.nextInt(100); r2[i] = r.nextInt(100); y = 42+r.nextInt(6); while(y==46) { y = 42+r.nextInt(6); } if(y==44) { r2[i] = 2+r.nextInt(8); } switch(y) { case 42: r3[i]="*"; sum[i]=r1[i]*r2[i]; System.out.print(sum[i]+"="+r1[i]+"*"+r2[i]); break; case 43: r3[i]="+"; sum[i]=r1[i]+r2[i]; System.out.println(sum[i]+"="+r1[i]+"+"+r2[i]); break; case 44: r3[i]="!"; sum[i]=1; for(int n=r2[i];n>1;n--) { sum[i]=sum[i]*n; } System.out.println(sum[i]+"="+r1[i]+"+"+r2[i]); break; case 45: r3[i]="-"; sum[i]=r1[i]-r2[i]; System.out.println(sum[i]+"="+r1[i]+"-"+r2[i]); break; case 47: r3[i]="/"; sum[i]=r1[i]/r2[i]; System.out.println(sum[i]+"="+r1[i]+"/"+r2[i]); break; } } return sum; } }
1.因为要求换组员,上次作业和27号同学一起完成,这次作业与37号施少涛同学一起完成。http://www.cnblogs.com/shishaotao/
2.黑盒测试:点击确定->点击确定->(将非法字符清空)输入->
白盒测试:
3.单元测试:我们以数组的形式,数组先获取随机数存储进去,然后以数组的形式输出。因此,单元测试对数组进行初始化。最终输入的结果与算的结果相符合
这些程序和测试是我和我的小伙伴合力一天完成的结果,有可能有些的地方还是不完善,毕竟是重新做的程序,而不是原来改装的。但大概的功能已实现。我希望老师能够多提点一些,我会在有空多余的时间把它完善。接下来说说我的感受吧,在第一次的结对合作时,就好像与同伴格格不入的样子,但是到了这次结对,我觉得好像有点感觉,有同伴的存在,进度好像快了些,问题的解决也快了。
我的同伴:35何武鹏 http://home.cnblogs.com/u/hewupeng/