结伴队友是12陈帝燊,博客地址:http://www.cnblogs.com/cds94/
这次有点赶,所以与之前的队友结对
主要代码:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import javax.swing.*; public class rrr extends JFrame implements ActionListener{ JLabel L1=new JLabel("题目:"); JLabel L2=new JLabel("输入题目个数:"); JLabel L3=new JLabel("输入答案"); JLabel L4=new JLabel(" "); JLabel L5=new JLabel(" "); JTextField T1=new JTextField(6); JTextField T2=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,ri,m=0,p; int a[]=new int[100]; int b[]=new int[100]; String c[]=new String[100]; String ch[]={"+","-","*","/"}; public static void main(String[] args) { new rrr(); // TODO Auto-generated method stub } rrr() { this.setTitle("四则运算"); this.setSize(300,350); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.add(p1); p1.setLayout(null); L2.setBounds(50, 20, 90, 20); p1.add(L2); T2.setBounds(150, 20, 90, 30); p1.add(T2); L1.setBounds(50, 70, 90, 30); p1.add(L1); L4.setBounds(150, 70, 90, 30); p1.add(L4); L3.setBounds(50,120, 90, 30); p1.add(L3); T1.setBounds(150, 120, 90, 30); p1.add(T1); T1.setEnabled(false); B1.setBounds(50, 170, 90, 30); p1.add(B1); B1.addActionListener(this); B2.setBounds(150, 170, 90, 30); p1.add(B2); B2.setEnabled(false); B2.addActionListener(this); B3.setBounds(50,220,90,30); p1.add(B3); B3.setEnabled(false); B3.addActionListener(this); B4.setBounds(150, 220,90,30); p1.add(B4); B4.setEnabled(false); B4.addActionListener(this); L5.setBounds(50, 270, 180, 30); p1.add(L5); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗体 B1.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==B1){ B1.setEnabled(false); B2.setEnabled(true); B3.setEnabled(true); B4.setEnabled(true); T2.setEnabled(false); T1.setEnabled(true); L5.setText(" "); m=0; ri=0; n=(int) Double.parseDouble(T2.getText()); if(n>5||n<1) { JOptionPane.showMessageDialog(null,"输入的个数不能大于5或者小于1","注意",JOptionPane.INFORMATION_MESSAGE); } else begin(); } if(e.getSource()==B2){ B1.setEnabled(true); B2.setEnabled(false); B3.setEnabled(false); B4.setEnabled(false); T2.setEnabled(true); T1.setText(""); T2.setText(""); } if(e.getSource()==B3){ begin(); T1.setText(""); B4.setEnabled(true); L5.setText(" "); } if(e.getSource()==B4){ answer(); B4.setEnabled(false); if(m==n){ B2.setEnabled(true); B3.setEnabled(false); B4.setEnabled(false); T1.setEditable(false); JOptionPane.showMessageDialog(null,"答对了"+ri+"题,答错了"+(n-ri)+"题","成绩",JOptionPane.INFORMATION_MESSAGE); } } } void begin(){ a[m]=(int)(Math.random()*200-100); b[m]=(int)(Math.random()*200-100); c[m]=ch[(int)(Math.random()*3)]; while(b[m]==0&&c[m]==ch[3]){ b[m]=(int)(Math.random()*200-100); } if(a[m]>=0&&b[m]>=0){ L4.setText(String.valueOf(a[m])+c[m]+String.valueOf(b[m])+"="); } if(a[m]<0&&b[m]>=0){ L4.setText("("+String.valueOf(a[m])+")"+c[m]+String.valueOf(b[m])+"="); } if(a[m]>=0&&b[m]<0){ L4.setText(String.valueOf(a[m])+c[m]+"("+String.valueOf(b[m])+")"+"="); } if(a[m]<0&&b[m]<0){ L4.setText("("+String.valueOf(a[m])+")"+c[m]+"("+String.valueOf(b[m])+")"+"="); } } void answer(){ calc re=new calc(); double an=Double.parseDouble(T1.getText()); if(re.cal(a[m], b[m], c[m])==an){ L5.setText("答案正确!"); ri++; } else{ L5.setText("答案错误!正确答案是"+re.cal(a[m], b[m], c[m])); } m++; } }
计算功能的算法封装成的类:
public class cal { int calc(int a,int b,String c){ int d = 0; if(c.equals("+")) d=add(a,b); if(c.equals("-")) d=minius(a,b); if(c.equals("*")) d=mult(a,b); if(c.equals("/")) d=divi(a,b); return d; } int add(int a,int b){ return a+b; } int minius(int a,int b){ return a-b; } int mult(int a,int b){ return a*b; } int divi(int a,int b){ return a/b; } }
单元测试:
import static org.junit.Assert.*; import org.junit.Test; public class calTest { cal test=new cal(); @Test public void testCal() { assertEquals(-4,test.cal(-12, 3,"/")); } @Test public void testCal2() { assertEquals(-9,test.cal(-12, 3,"+")); } @Test public void testCal3() { assertEquals(-15,test.cal(-12, 3,"-")); } @Test public void testCal4() { assertEquals(-36,test.cal(-12, 3,"*")); } @Test public void testAdd() { assertEquals(9,test.add(3, 6)); } @Test public void testMinius() { assertEquals(-6,test.minius(3, 9)); } @Test public void testMult() { assertEquals(-27,test.mult(-3, 9)); } @Test public void testDivi() { assertEquals(5,test.divi(25, 5)); } }
测试结果: