import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.math.RoundingMode; import java.util.Random; import java.util.regex.Pattern; import javax.swing.JButton; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; /* * Test.java * * Created on __DATE__, __TIME__ */ /** * * @author __USER__ */ public class Test extends javax.swing.JFrame { public String tmnum;//输入的题目数量 public int n=0;//实际拿来计算的题目数量 public String s[] = new String[5]; Pattern pattern = Pattern.compile("[0-9]*"); public int result[] = new int[5]; private JPanel panel; static JTextField[] textField = new JTextField[5]; long startTime; long endTime; long time; JLabel labelresult = new JLabel("提交后结果在这里展示"); /** Creates new form Test */ public Test() { super("四则运算"); getContentPane().setBackground(new Color(255, 255, 0)); setBackground(new Color(255, 255, 255)); setForeground(new Color(255, 255, 255)); this.setSize(700, 600); this.setLocation(350, 50);// 主窗口位置 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(true); tmnum = JOptionPane.showInputDialog("请输入题目个数(题目数不能大于5):"); while (true) { if (pattern.matcher(tmnum).matches()) {// 如果输入的是数字就执行 n = Integer.parseInt(tmnum); if (n <= 0 || n >= 6) { tmnum = JOptionPane.showInputDialog("输入错误!(题目数不能大于5):"); } else { n = Integer.parseInt(tmnum); break; } } else { tmnum = JOptionPane.showInputDialog("输入错误!(题目数不能大于5):"); } } ; initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ //GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { timu(); panel = new JPanel(); panel.setForeground(new Color(0, 0, 0)); panel.setBackground(new Color(253, 245, 230)); getContentPane().add(panel, BorderLayout.CENTER); GridBagLayout gbl_p1 = new GridBagLayout(); gbl_p1.columnWidths = new int[] { 0, 0, 0 }; gbl_p1.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; gbl_p1.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE }; gbl_p1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE }; panel.setLayout(gbl_p1); for (int i = 0; i < n; i++) { JLabel label = new JLabel(s[i]);// 第一题标签 label.setBackground(new Color(144, 238, 144)); label.setFont(new Font("Batang", Font.PLAIN, 24)); label.setHorizontalAlignment(SwingConstants.LEFT); GridBagConstraints gbc_label = new GridBagConstraints(); gbc_label.anchor = GridBagConstraints.SOUTH; gbc_label.fill = GridBagConstraints.HORIZONTAL; gbc_label.insets = new Insets(0, 20, 20, 0); gbc_label.gridx = 0; gbc_label.gridy = i+1; panel.add(label, gbc_label); textField[i] = new JTextField(10); GridBagConstraints gbc_textField = new GridBagConstraints(); gbc_textField.anchor = GridBagConstraints.SOUTH; gbc_textField.insets = new Insets(0, 0, 20, 10); gbc_textField.fill = GridBagConstraints.HORIZONTAL; gbc_textField.gridx = 1; gbc_textField.gridy = i + 1; textField[i].setColumns(10); panel.add(textField[i], gbc_textField); } labelresult.setBackground(new Color(144, 238, 144)); labelresult.setFont(new Font("Batang", Font.PLAIN, 24)); labelresult.setHorizontalAlignment(SwingConstants.LEFT); GridBagConstraints gbc_label = new GridBagConstraints(); gbc_label.anchor = GridBagConstraints.SOUTH; gbc_label.fill = GridBagConstraints.HORIZONTAL; gbc_label.insets = new Insets(0, 20, 20, 0); gbc_label.gridx = 0; gbc_label.gridy = 6; panel.add(labelresult, gbc_label); JButton jButton1 = new JButton("提交");// 提交按钮 GridBagConstraints gbc_btnNewButton = new GridBagConstraints(); gbc_btnNewButton.anchor = GridBagConstraints.NORTHWEST; gbc_btnNewButton.insets = new Insets(0, 100, 20, 40); gbc_btnNewButton.gridx = 0; gbc_btnNewButton.gridy = 0; panel.add(jButton1, gbc_btnNewButton); JButton jButton2 = new JButton("退出");// 退出按钮 GridBagConstraints gbc_button = new GridBagConstraints(); gbc_button.insets = new Insets(0, 0, 20, 40); gbc_button.anchor = GridBagConstraints.NORTHWEST; gbc_button.gridx = 1; gbc_button.gridy = 0; panel.add(jButton2, gbc_button); JButton jButton3 = new JButton("换肤"); GridBagConstraints gbc_hybutton = new GridBagConstraints(); gbc_hybutton.anchor = GridBagConstraints.NORTHWEST; gbc_hybutton.insets = new Insets(0, 0, 0, 5); gbc_hybutton.gridx = 2; gbc_hybutton.gridy = 0; panel.add(jButton3, gbc_hybutton); startTime = System.currentTimeMillis();// 开始时间 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); pack(); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { panel.setBackground(new Color(254, 235, 150)); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { endTime = System.currentTimeMillis(); time = endTime - startTime; int time1 = (int) (time / 1000); String s = "一共用时:"+time1+"s"; int count=0; for (int i = 0; i < n; i++) { String str = textField[i].getText(); String result = String.valueOf(this.result[i]); if(result.equals(str)){ count +=1; } } s += "一共答对"+count+"题"; labelresult.setText(s); labelresult.setFont(new Font("仿宋", Font.PLAIN, 20)); } public void timu() {// 题目 char[] cal = { '+', '-', '*', '/' }; Random ra = new Random(); int paramType;// 数值类型 int topicType;// 题目类型 int a; int b; char c; int result = 0; String str = new String(); for (int i = 0; i < n; i++) { topicType = ra.nextInt(2); if (topicType == 0) {// 正常四则运算 paramType = ra.nextInt(2); if (paramType == 0){// 负数 a = -ra.nextInt(100);// 控制随机数数值 } else{// 正数 a = ra.nextInt(100);// 控制随机数数值 } paramType = ra.nextInt(2); if (paramType == 0){// 负数 b = -ra.nextInt(100);// 控制随机数数值 } else{// 正数 b = ra.nextInt(100);// 控制随机数数值 } int index = ra.nextInt(cal.length);// 生成符号,随机数 c = cal[index]; if(a<0){ str += "("+a+")"; } else { str += ""+a; } str += c; if(b<0){ str += "("+b+")="; } else { str += b+"="; } s[i] = str; str = new String(); switch (c) {// 计算随机数的值 case '+': { result = a + b; } break; case '-': { result = a - b; } break; case '*': { result = a * b; } break; case '/': { result = a / b; } break; } this.result[i] = result; } else{ int jiechengnum = ra.nextInt(6); s[i] = jiechengnum + "!="; int jiechengResult=1; if (jiechengnum == 0) { this.result[i] = 1; } else { for (int o = 1; o <= jiechengnum; o++) { jiechengResult = jiechengResult*o; } this.result[i]=jiechengResult; } } } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Test().setVisible(true); } }); } }
开发环境:eclipse
开始界面:允许用户输入题数。
用户输入答案,此时按提交后系统将会算出用户答对的题数和所用时间。
显示时间和正确题数。
这是换肤后的界面。
总结:
经过2天的努力,最后我们在参考资料和自己不断测试的条件下,写出了改进版的“四则运算”。
这个四则运算完成了老师的最低要求,而且还有4个扩展方向:1.界面换肤;2:用户控制题目数量;3:计算用户答题时间;4:计算用户答对多少题。
每一次的程序都是一次考验,不仅要求写代码的同学要注重细节,更要求团队中的合作精神,当同伴卡在某一个逻辑困境的时候应该及时告诉同伴怎样才是合理的,这样才能写出合理有用的代码。
Partner:
黄伟俊 201306114423 http://www.cnblogs.com/hwj23/
陈汝婷 201306114454 http://i.cnblogs.com/EditPosts.aspx?opt=1