标签:
这次的结对伙伴是:03游金浪,博客:http://www.cnblogs.com/santokim/
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import javax.swing.*; public class aaa 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 aaa(); // TODO Auto-generated method stub } aaa() { 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(){ cal re=new cal(); double an=Double.parseDouble(T1.getText()); if(re.calc(a[m], b[m], c[m])==an){ L5.setText("答案正确!"); ri++; } else{ L5.setText("答案错误!正确答案是"+re.calc(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; } }
单元测试:
标签:
原文地址:http://www.cnblogs.com/cds94/p/4488569.html