开发环境:eclipse
结对同伴 :罗凯杰
同伴博客:http://www.cnblogs.com/lkjie/
未封装的程序:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Size { public static void main(String[] args) { // TODO Auto-generated method stub myframe f=new myframe(); } } class myframe extends JFrame { JLabel L1=new JLabel("题目:"); JLabel L2=new JLabel("输入题目个数:"); JLabel L3=new JLabel("提示:"); JLabel L4=new JLabel(" "); //题目 JLabel L5=new JLabel("题目个数不能大于5"); //提示 JTextField T1=new JTextField(6); JButton B1=new JButton("开始"); JButton B2=new JButton("确认"); JButton B3=new JButton("下一题"); JButton B4=new JButton("换皮肤"); JPanel p1=new JPanel(); int n=0,a,b,c,d,j,k,l,m=0,p; double result; char[]h={‘+‘,‘-‘,‘*‘,‘/‘},h1={‘(‘,‘)‘,‘!‘}; String s1,s2; Color col[]={Color.blue,Color.green,Color.ORANGE,Color.PINK,Color.GRAY,Color.RED,Color.LIGHT_GRAY}; myframe(){ this.setTitle("四则运算"); this.setSize(300,280); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.add(p1); p1.setLayout(null); L1.setBounds(50, 20, 90, 30); p1.add(L1); L4.setBounds(140, 20, 150, 30); p1.add(L4); L2.setBounds(50, 70, 90, 30); p1.add(L2); T1.setBounds(150, 70, 90, 30); p1.add(T1); B4.setBounds(100, 110, 90, 30); p1.add(B4); B1.setBounds(10, 150, 90, 30); p1.add(B1); B2.setBounds(100, 150, 90, 30); p1.add(B2); B2.setEnabled(false); B3.setBounds(190, 150,90,30); p1.add(B3); L3.setBounds(50, 200, 50, 30); B3.setEnabled(false); p1.add(L3); L5.setBounds(100, 200, 180, 30); p1.add(L5); this.setVisible(true); this.setLocationRelativeTo(null); B1.addActionListener(new B()); B2.addActionListener(new B()); B3.addActionListener(new B()); B4.addActionListener(new B()); } class B implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==B1) { p=n=(int) Double.parseDouble(T1.getText()); //System.out.print(n); if(n>5||n<1) { JOptionPane.showMessageDialog(null,"输入的个数不能大于5或者小于1","注意",JOptionPane.INFORMATION_MESSAGE); } else { B1.setEnabled(false); B2.setEnabled(true); B3.setEnabled(true); L2.setText("输入答案 :"); L5.setText(null); T1.setText(null); star(); } } if(e.getSource()==B2) { l=(int) Double.parseDouble(T1.getText()); if(l==result) { m++; s2="答案正确!"; L5.setText(s2); } else{ s2="答案错误!答案为:"+String.valueOf(result); L5.setText(s2); } B2.setEnabled(false); } if(e.getSource()==B3) { if(n==0) { JOptionPane.showMessageDialog(null, "一共"+p+"道题目。正确的题数为:"+m,"成绩",JOptionPane.INFORMATION_MESSAGE); B1.setEnabled(true); B2.setEnabled(false); B3.setEnabled(false); L2.setText("输入题目个数:"); L4.setText(null); T1.setText(null); } else { L5.setText(null); T1.setText(null); star(); B2.setEnabled(true); } } if(e.getSource()==B4) { int i; i=(int)(Math.random()*6); p1.setBackground(col[i]); } } } void star() { a=(int) (Math.random()*200-100); b=(int) (Math.random()*200-100); c=(int) (Math.random()*200-100); d=(int) (Math.random()*200-100); j=(int) (Math.random()*4); k=(int) (Math.random()*4); if(d<0) { a=(int) (Math.random()*9+1); s1=String.valueOf(a)+String.valueOf(h1[2]); L4.setText(s1); result=1; for(int i=1;i<=a;i++) { result=result*i; } } else { if(b>=0&&c>=0) { s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(b)+String.valueOf(h[k])+String.valueOf(c); L4.setText(s1); System.out.println(s1); } else if(b<0&&c>=0) { s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(h1[0])+String.valueOf(b)+String.valueOf(h1[1])+String.valueOf(h[k])+String.valueOf(c); L4.setText(s1); System.out.println(s1); } else if(b>=0&&c<0) { s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(b)+String.valueOf(h[k])+String.valueOf(h1[0])+String.valueOf(c)+String.valueOf(h1[1]); L4.setText(s1); System.out.println(s1); } else { s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(h1[0])+String.valueOf(b)+String.valueOf(h1[1])+String.valueOf(h[k])+String.valueOf(h1[0])+String.valueOf(c)+String.valueOf(h1[1]); L4.setText(s1); System.out.println(s1); } if(k>1) //后面的计算符为乘除,先算后面两个数 { if(k==2) { result=b*c; } else if(k==3) { result=b/c; } if(j==0) { result=result+a; } else if(j==1) { result=a-result; } else if(j==2) { result=a*result; } else if(j==3) { result=a/result; } } else if(k<2) { if(j==0) { result=b+a; } else if(j==1) { result=a-b; } else if(j==2) { result=a*b; } else if(j==3) { result=a/b; } if(k==0) result=result+c; if(k==1) result=result-c; } } System.out.println(result); n--; } }
封装后的代码:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Size { public static void main(String[] args) { // TODO Auto-generated method stub myframe f=new myframe(); } } class myframe extends JFrame { JLabel L1=new JLabel("题目:"); JLabel L2=new JLabel("输入题目个数:"); JLabel L3=new JLabel("提示:"); JLabel L4=new JLabel(" "); //题目 JLabel L5=new JLabel("题目个数不能大于5"); //提示 JTextField T1=new JTextField(6); JButton B1=new JButton("开始"); JButton B2=new JButton("确认"); JButton B3=new JButton("下一题"); JButton B4=new JButton("换皮肤"); JPanel p1=new JPanel(); int n=0,a,b,c,d,j,k,l,m=0,p; double result; char[]h={‘+‘,‘-‘,‘*‘,‘/‘},h1={‘(‘,‘)‘,‘!‘}; String s1,s2; Color col[]={Color.blue,Color.green,Color.ORANGE,Color.PINK,Color.GRAY,Color.RED,Color.LIGHT_GRAY}; myframe(){ this.setTitle("四则运算"); this.setSize(300,280); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.add(p1); p1.setLayout(null); L1.setBounds(50, 20, 90, 30); p1.add(L1); L4.setBounds(140, 20, 150, 30); p1.add(L4); L2.setBounds(50, 70, 90, 30); p1.add(L2); T1.setBounds(150, 70, 90, 30); p1.add(T1); B4.setBounds(100, 110, 90, 30); p1.add(B4); B1.setBounds(10, 150, 90, 30); p1.add(B1); B2.setBounds(100, 150, 90, 30); p1.add(B2); B2.setEnabled(false); B3.setBounds(190, 150,90,30); p1.add(B3); L3.setBounds(50, 200, 50, 30); B3.setEnabled(false); p1.add(L3); L5.setBounds(100, 200, 180, 30); p1.add(L5); this.setVisible(true); this.setLocationRelativeTo(null); B1.addActionListener(new B()); B2.addActionListener(new B()); B3.addActionListener(new B()); B4.addActionListener(new B()); } class B implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==B1) { p=n=(int) Double.parseDouble(T1.getText()); //System.out.print(n); if(n>5||n<1) { JOptionPane.showMessageDialog(null,"输入的个数不能大于5或者小于1","注意",JOptionPane.INFORMATION_MESSAGE); } else { B1.setEnabled(false); B2.setEnabled(true); B3.setEnabled(true); L2.setText("输入答案 :"); L5.setText(null); T1.setText(null); Star.star(h1, h,a,b,c,d,n,j,k,s1,L4,result); } } if(e.getSource()==B2) { l=(int) Double.parseDouble(T1.getText()); if(l==result) { m++; s2="答案正确!"; L5.setText(s2); } else{ s2="答案错误!答案为:"+String.valueOf(result); L5.setText(s2); } B2.setEnabled(false); } if(e.getSource()==B3) { if(n==0) { JOptionPane.showMessageDialog(null, "一共"+p+"道题目。正确的题数为:"+m,"成绩",JOptionPane.INFORMATION_MESSAGE); B1.setEnabled(true); B2.setEnabled(false); B3.setEnabled(false); L2.setText("输入题目个数:"); L4.setText(null); T1.setText(null); } else { L5.setText(null); T1.setText(null); Star.star(h1, h,a,b,c,d,n,j,k,s1,L4,result); B2.setEnabled(true); } } if(e.getSource()==B4) { int i; i=(int)(Math.random()*6); p1.setBackground(col[i]); } } } }
import javax.swing.JLabel; public class Star { public static void star(char[]h1,char[]h,int a,int b,int c,int d,int n,int j,int k,String s1,JLabel L4,double result){ a=(int) (Math.random()*200-100); b=(int) (Math.random()*200-100); c=(int) (Math.random()*200-100); d=(int) (Math.random()*200-100); j=(int) (Math.random()*4); k=(int) (Math.random()*4); if(d<0) { a=(int) (Math.random()*9+1); s1=String.valueOf(a)+String.valueOf(h1[2]); L4.setText(s1); result=1; for(int i=1;i<=a;i++) { result=result*i; } } else { if(b>=0&&c>=0) { s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(b)+String.valueOf(h[k])+String.valueOf(c); L4.setText(s1); System.out.println(s1); } else if(b<0&&c>=0) { s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(h1[0])+String.valueOf(b)+String.valueOf(h1[1])+String.valueOf(h[k])+String.valueOf(c); L4.setText(s1); System.out.println(s1); } else if(b>=0&&c<0) { s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(b)+String.valueOf(h[k])+String.valueOf(h1[0])+String.valueOf(c)+String.valueOf(h1[1]); L4.setText(s1); System.out.println(s1); } else { s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(h1[0])+String.valueOf(b)+String.valueOf(h1[1])+String.valueOf(h[k])+String.valueOf(h1[0])+String.valueOf(c)+String.valueOf(h1[1]); L4.setText(s1); System.out.println(s1); } if(k>1) //后面的计算符为乘除,先算后面两个数 { if(k==2) { result=b*c; } else if(k==3) { result=b/c; } if(j==0) { result=result+a; } else if(j==1) { result=a-result; } else if(j==2) { result=a*result; } else if(j==3) { result=a/result; } } else if(k<2) { if(j==0) { result=b+a; } else if(j==1) { result=a-b; } else if(j==2) { result=a*b; } else if(j==3) { result=a/b; } if(k==0) result=result+c; if(k==1) result=result-c; } } System.out.println(result); n--; } }
白盒测试:在输入非法字符的时候会提示出错,在规定的条件下才能运行。
黑盒测试:运行后在提交答案的时候出现错误,无法传递正确答案。
收获到和感悟:
在这次编写的封装中,有很多的地方还是弄不懂,编写的过程都是跟同伴一起完成的,遇到不会都是共同思考讨论得出结果,所以结对编程又给我加深了印象,在原来个人需要花费的时间大大减少了。对于单元测试这个方面还不是很理解,需要多点练习。
时间: 2024-11-03 21:58:16