Four-Operations

开发环境:Eclipse

结对小伙伴:201306114416 陈键 (http://www.cnblogs.com/be-the-one/

               201306114452 吴舒婷 (http://www.cnblogs.com/wst-2015/

功能:

        a)生成题目,单个题目最多不能超过4个运算符,操作数小于100。

    b)用户可以输入答案。

        c)用户输入答案正确,正确提示;错误,则提示正确答案。

扩展方向: 

      b.可以出表达式里含有负整数(负整数最小不小于-100)的题目,且负数需要带括号,用户输入的结果不用带括号。如: 2*(-4) = -8

      c.用户答题结束以后,程序可以显示用户答题所用的时间

      d.用户可以选择出题的个数(最多不能超过5个题目),答题结束可以显示用户答错的题目个数和答对的题目个数

      e.用户在第一次答题,需要用户注册用户名,下次启动,程序记住用户前一次输入的用户名

    f.程序可以出单个整数阶乘的题目:如:4!=24

      g.程序可以设置答题时间,时间设置为整数,单位为秒,最大不能超过120秒,若超过了答题时间未答题,则提示:时间已到,不能答题。(默认为120seconds,在后台倒计时)

      h.程序可以设置皮肤功能,可以改变界面的颜色即可。(右键跟换皮肤)

任务分配:

     我的结对同伴主要负责多一点,界面框架,线程(c,g,h)等.而我就是负责算法还有整合其他模块.So在结对时间,一般都是看着我们的驾驶员如何安全行驶.在整合的时候就会整体的审查一次,检查是否有什么不合理的代码存在.

感想:

对于收获我觉得还是挺多的,结对工作的却是能减少编程中所犯错误,程序质量也是会相对高一点.最明显的就是在这次编程中没有出现个人编辑时出现的卡在某个错误点致使不能运行的情况.事实就是这样每个人擅长的方面都是不一样的,结对工作也是结对学习的过程.在这个过程中相互提醒,通过互补来提高程序的总体质量.再者就正如书中所说,两人合作是分阶段性的,总会遇到这样的”不爽”.彼此都有自己的编程风格,但是有时候,个人觉得小如一个变量的定义也是可以决定或者说改变很多东西的.在此次合作中就碰到一次关于变量定义的邂逅.所以还是要注意重视编程的style.最后就是关于同伴之间的交流了,先是肯定partner的成果,再指出不合之处.不然个人觉得这个合作完之后应该就不能好好做朋友了.总之组队better than 单干.

import java.awt.*;
import java.awt.event.*;
import java.io.*;


import javax.swing.*;
public class LayerJFrame extends JFrame {
    private static final long serialVersionUID = -8620276339887152797L;
    static JFrame jframe=null;
     JLabel Luser=null;
     int flag=0;
     static JTextField Tuser=null;
     
     static JButton ok=null;
     static JButton cancell=null;

     public void init(){
         jframe=new JFrame();
         
         Luser=new JLabel("帐号:");
         Luser.setBounds(new Rectangle(40,20,70,30));
         Luser.setForeground(Color.black);
         Luser.setFont(new Font("楷体",20,20));
         
         
         Tuser=new JTextField();
         Tuser.setBounds(new Rectangle(100,20,150,30));
         Tuser.setFont(new Font("楷体",20,20));
        
         ok=new JButton("登陆");
         ok.setBounds(new Rectangle(80,70,70,30));
         cancell=new JButton("注册");
         cancell.setBounds(new Rectangle(190,70,70,30));
         
         jframe.add(Luser);     
         jframe.add(Tuser);
       
         jframe.add(ok);
         jframe.add(cancell);
         jframe.getContentPane().setBackground(Color.green);
         jframe.setLayout(null);
         jframe.setVisible(true);     
         jframe.setBounds(new Rectangle(450,250,330,150));
         jframe.setResizable(false);
         
         ok.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
                 try {            
                 String user=Tuser.getText();                
                 BufferedReader br=new BufferedReader(new FileReader("data.txt"));
                 String temp=null;            
                 do{
                     temp=br.readLine();
                     if(temp==null){
                         JOptionPane.showMessageDialog(jframe, "用户不存在,请先注册。");
                         break;
                     }
                     if(temp.equals(user))
                         {
                         flag=2;
                     break;
                 }
                     System.out.print(temp);
             }while(temp!=null);
                 
                 if(flag==2){
                    //    jframe.setVisible(false);
                            EventQueue.invokeLater(new Runnable() {
                                public void run() {
                                    try {
                                        Input window = new Input();
                                        window.frame.setVisible(true);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                    }
                 } catch (Exception e1) {                    
                     e1.printStackTrace();
                 }                
            }
          });
         cancell.addActionListener(new ActionListener(){  
             public void actionPerformed(ActionEvent e) {
                  try {
                      String user=Tuser.getText();    
                    BufferedWriter bw=new BufferedWriter(new FileWriter("data.txt"));
                    bw.write(user);
                    bw.newLine();
                    JOptionPane.showMessageDialog(jframe, "用户创建成功!");
                    bw.flush();
                    bw.close();    
            } catch (Exception e1) {                    
                    e1.printStackTrace();
                    }
                }        
         });
         }
     public LayerJFrame(){
         init();
     }
     public static void main(String[] args){
         new LayerJFrame();
         
     }
        
} 
登陆界面
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JComboBox;
import java.awt.Color;
import javax.swing.DefaultComboBoxModel;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.JButton;


public class Input {
    public JFrame frame;
    String jud1,jud2,jud3;
    int number;
    JComboBox comboBox ;
    JCheckBox checkBox;
    JCheckBox checkBox_1;
    JCheckBox checkBox_2;
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Input window = new Input();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Input() {
        initialize();
    }
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(560, 150, 150, 220);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        FlowLayout fl_panel = new FlowLayout(FlowLayout.CENTER, 5, 5);
        panel.setLayout(fl_panel);
        
        JLabel lblNewLabel = new JLabel("\u9898\u76EE\u4E2A\u6570\uFF1A");
        lblNewLabel.setForeground(Color.BLUE);
        panel.add(lblNewLabel);
        
         comboBox = new JComboBox();
        comboBox.setForeground(Color.BLUE);
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5"}));
        comboBox.setSelectedIndex(0);
        panel.add(comboBox);
        
        checkBox = new JCheckBox("\u662F\u5426\u6709\u4E58\u9664");
        checkBox.setVerticalAlignment(SwingConstants.BOTTOM);
        checkBox.setForeground(Color.BLUE);
        panel.add(checkBox);
        
         checkBox_1 = new JCheckBox("\u662F\u5426\u6709\u5C0F\u6570");
        checkBox_1.setVerticalAlignment(SwingConstants.TOP);
        checkBox_1.setForeground(Color.BLUE);
        panel.add(checkBox_1);
        
         checkBox_2 = new JCheckBox("\u662F\u5426\u6709\u8D1F\u6570");
        checkBox_2.setForeground(Color.BLUE);
        panel.add(checkBox_2);
        
        JButton button = new JButton("\u5F00\u59CB\u505A\u9898");
        button.setForeground(Color.BLUE);
        panel.add(button);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                number=comboBox.getSelectedIndex()+1;
                if(checkBox.isSelected())
                    jud1="y";                
                else 
                    jud1="n";
                if(checkBox_1.isSelected())
                    jud2="y";                
                else 
                    jud2="n";
                if(checkBox_2.isSelected())
                    jud3="y";                
                else 
                    jud3="n";
                new Datas(number,jud1,jud2,jud3);
                //frame.setVisible(false);
            }
        });
    }

}
选择窗口
import java.util.Random;
import java.util.Scanner;

public class Datas {
    Random numbers;
    int number;
    int number2;
    int up;
    int down;
    int score = 0;// grades
    int count = 0;// rank of the pratice
    String jud1, jud2, jud3;

    public Datas(int data,String jud1,String jud2,String jud3){        
        String[] qustion= new String[data];
        float[] answer = new float[data];// computer's answer
        float[] answer2 = new float[data];// user's answer    
        
        Scanner in = new Scanner(System.in);
        numbers=new Random();
         number = numbers.nextInt(5)+1;
         number=data;
        number2 = 0;
        up = 10;
        down = 1;    
        this.jud1 =jud1; //multiply
        this.jud2=jud2;//point
        this.jud3 = jud3;//pessimistic
    
        
        for (int x = 0; x < qustion.length; x++)
            qustion[x] = new String("");    
    do {
        int XB2 = 0;// index
        int XB = 0;
        float num[] = new float[10];// count from 0 for numbers
        String sign[] = new String[6];// count from 0 for signs
        int flag = 2; // operation's length
        int shuzi;
        float shuzi2;
        int signal = 0;// optimistic pessmistic
        int b; // operator signal
        int point;// pointed number
        String Temp = new String("Y");
        int lucknumber=numbers.nextInt(8);    
        
        
        if(lucknumber>=2)
        {
            flag = (int) (Math.random() * 4) + 1;// operations' length
            if (jud2.equalsIgnoreCase(Temp))// whether there is a point
            {
                point = (int) (Math.random() * 5);
                if (jud3.equalsIgnoreCase(Temp))// pessimistic
                    signal = (int) (Math.random() * 5);
                if (point < 3) {
                    shuzi2 = (float) (Math.random() * (up - down)) + down;
                    shuzi2 = (int) (shuzi2 * 100);
                    shuzi2 = shuzi2 / 100;// two points
                } else
                    shuzi2 = (int) (Math.random() * (up - down)) + down;
                if (signal < 3) {
                    num[XB] = shuzi2;
                    XB++;
                    qustion[number2] = String.valueOf(shuzi2);// no point
                } else {
                    num[XB] = -shuzi2;
                    XB++;
                    qustion[number2] = "(-" + shuzi2 + ")";
                }
            } else {
                if (jud3.equalsIgnoreCase(Temp))
                    signal = (int) (Math.random() * 5);

                shuzi = (int) (Math.random() * (up - down) + down);
                if (signal < 3) {
                    num[XB] = shuzi;
                    XB++;
                    qustion[number2] = String.valueOf(shuzi);
                } else {
                    num[XB] = -shuzi;
                    XB++;
                    qustion[number2] = "(-" + shuzi + ")";
                }
            }
            // the first operator above
            do {
                if (jud1.equalsIgnoreCase(Temp))// whether has multiply,divide
                    b = (int) (Math.random() * 4 + 1);
                else
                    b = (int) (Math.random() * 2 + 1);
                switch (b) {
                case 1:// add
                    if (jud2.equalsIgnoreCase(Temp))// little point
                    {
                        point = (int) (Math.random() * 5);
                        if (jud3.equalsIgnoreCase(Temp))// pessimistic
                            signal = (int) (Math.random() * 5);
                        if (point < 2) {
                            shuzi2 = (float) (Math.random() * (up - down))
                                    + down;
                            shuzi2 = (int) (shuzi2 * 100);
                            shuzi2 = shuzi2 / 100;
                        } else
                            shuzi2 = (int) (Math.random() * (up - down)) + down;
                        if (signal < 3) {
                            num[XB] = shuzi2;
                            XB++;
                            sign[XB2] = "+";
                            XB2++;
                            qustion[number2] = qustion[number2] + "+"
                                    + String.valueOf(shuzi2);
                        } else {
                            num[XB] = -shuzi2;
                            XB++;
                            sign[XB2] = "+";
                            XB2++;
                            qustion[number2] = qustion[number2] + "+" + "(-"
                                    + shuzi2 + ")";
                        }
                    } else {

                        if (jud3.equalsIgnoreCase(Temp))// pessimistic
                            signal = (int) (Math.random() * 5);

                        shuzi = (int) (Math.random() * (up - down) + down);
                        if (signal < 3) {
                            num[XB] = shuzi;
                            XB++;
                            sign[XB2] = "+";
                            XB2++;
                            qustion[number2] = qustion[number2] + "+" + shuzi;
                        } else {
                            num[XB] = -shuzi;
                            XB++;
                            sign[XB2] = "+";
                            XB2++;
                            qustion[number2] = qustion[number2] + "+" + "(-"
                                    + shuzi + ")";
                        }
                    }
                    break;
                case 2:// less
                    if (jud2.equalsIgnoreCase(Temp)) {
                        point = (int) (Math.random() * 5);
                        if (jud3.equalsIgnoreCase(Temp))// pessimistic
                            signal = (int) (Math.random() * 5);
                        if (point < 2) {
                            shuzi2 = (float) (Math.random() * (up - down))
                                    + down;
                            shuzi2 = (int) (shuzi2 * 100);
                            shuzi2 = shuzi2 / 100;
                        } else
                            shuzi2 = (int) (Math.random() * (up - down)) + down;
                        if (signal < 3) {
                            num[XB] = shuzi2;
                            XB++;
                            sign[XB2] = "-";
                            XB2++;
                            qustion[number2] = qustion[number2] + "-" + shuzi2;
                        } else {
                            num[XB] = -shuzi2;
                            XB++;
                            sign[XB2] = "-";
                            XB2++;
                            qustion[number2] = qustion[number2] + "-" + "(-"
                                    + shuzi2 + ")";
                        }
                    } else {
                        if (jud3.equalsIgnoreCase(Temp))// pessimistic
                            signal = (int) (Math.random() * 5);

                        shuzi = (int) (Math.random() * (up - down) + down);
                        if (signal < 3) {
                            num[XB] = shuzi;
                            XB++;
                            sign[XB2] = "-";
                            XB2++;
                            qustion[number2] = qustion[number2] + "-" + shuzi;
                        } else {
                            num[XB] = -shuzi;
                            XB++;
                            sign[XB2] = "-";
                            XB2++;
                            qustion[number2] = qustion[number2] + "-" + "(-"
                                    + shuzi + ")";
                        }
                    }
                    break;
                case 3:// multiply
                    if (jud2.equalsIgnoreCase(Temp)) {
                        point = (int) (Math.random() * 5);
                        if (jud3.equalsIgnoreCase(Temp))// pessimistic
                            signal = (int) (Math.random() * 5);
                        if (point < 2) {
                            shuzi2 = (float) (Math.random() * (up - down))
                                    + down;
                            shuzi2 = (int) (shuzi2 * 100);
                            shuzi2 = shuzi2 / 100;
                        } else
                            shuzi2 = (int) (Math.random() * (up - down)) + down;
                        if (signal < 3) {
                            num[XB - 1] = shuzi2 * num[XB - 1];
                            qustion[number2] = qustion[number2] + "*" + shuzi2;
                        } else {
                            num[XB - 1] = shuzi2 * num[XB - 1];
                            qustion[number2] = qustion[number2] + "*" + shuzi2;
                        }
                    } else {
                        if (jud3.equalsIgnoreCase(Temp))// pessimistic
                            signal = (int) (Math.random() * 5);
                        shuzi = (int) (Math.random() * (up - down) + down);
                        if (signal < 3) {
                            num[XB - 1] = shuzi * num[XB - 1];
                            qustion[number2] = qustion[number2] + "*" + shuzi;
                        } else {
                            num[XB - 1] = shuzi * num[XB - 1];
                            qustion[number2] = qustion[number2] + "*" + "(-"
                                    + shuzi + ")";
                        }
                    }
                    break;
                case 4:// divide
                    if (jud2.equalsIgnoreCase(Temp)) {
                        point = (int) (Math.random() * 5);
                        if (jud3.equalsIgnoreCase(Temp))// pessimistic
                            signal = (int) (Math.random() * 5);
                        if (point < 2) {
                            shuzi2 = (float) (Math.random() * (up - down))
                                    + down;
                            shuzi2 = (int) (shuzi2 * 100);
                            shuzi2 = shuzi2 / 100;
                        } else
                            shuzi2 = (int) (Math.random() * (up - down)) + down;
                        if (signal < 3) {
                            num[XB - 1] = num[XB - 1] / shuzi2;
                            qustion[number2] = qustion[number2] + "÷" + shuzi2;
                        } else {
                            num[XB - 1] = num[XB - 1] / shuzi2;
                            qustion[number2] = qustion[number2] + "÷" + "(-"
                                    + shuzi2 + ")";
                        }
                    } else {
                        if (jud3.equalsIgnoreCase(Temp))// pessimistic
                            signal = (int) (Math.random() * 5);

                        shuzi = (int) (Math.random() * (up - down) + down);
                        if (signal < 3) {
                            num[XB - 1] = num[XB - 1] / shuzi;
                            qustion[number2] = qustion[number2] + "÷" + shuzi;
                        } else {
                            num[XB - 1] = num[XB - 1] / shuzi;
                            qustion[number2] = qustion[number2] + "÷" + "(-"
                                    + shuzi + ")";
                        }
                    }
                    break;
                }
                flag--;
            } while (flag > 0);
            answer[number2] = calculation(sign, num);        
        }
        else if(lucknumber<=1)
        {
            answer[number2]=1;
            int lucky=(int)(Math.random()*6)+1;
            qustion[number2]=String.valueOf(lucky)+"!";
            for(int x=1;x<=lucky;x++)
                answer[number2]=x*answer[number2];
        }        
        number2++;
    } while (number > number2);
    
    new MyJFrame(qustion,answer,number);
    
    }
    // calculations
static float calculation(String a[]/* sign array */, float b[]/* operator array */) {
    float answer = 0;
    for (int i = 0; i < a.length; i++)
        if (a[i] != null)
            switch ((isoperator(a[i].charAt(0)))) {
            case 1:
                b[0] = b[0] + b[1];
                for (int j = 1; j < b.length - 1; j++)
                    if (b[j + 1] != 0) {
                        b[j] = b[j + 1];
                        b[j + 1] = 0;
                    } else
                        break;
                break;
            case 2:
                b[0] = b[0] - b[1];
                for (int j = 1; j < b.length - 1; j++)
                    if (b[j + 1] != 0) {
                        b[j] = b[j + 1];
                        b[j + 1] = 0;
                    } else
                        break;
                break;
            default:
                break;
            }
        else
            break;
    answer = b[0];
    answer = Math.round(answer * 100);
    answer = answer / 100;
    return answer;// Computer answer
}
// judge whether it is a sign
static int isoperator(char c) {
    if (c == '+')
        return 1;
    else if (c == '-')
        return 2;
    else if (c == '/')
        return 3;
    return 0;
}
    }


        
        
    
产生算式
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Scanner;
import javax.swing.*;

public class MyJFrame extends JFrame implements ActionListener, Runnable {
    JLabel Exercise1, Exercise2, Exercise3, Exercise4, Exercise5;
    JLabel LCorrect, LTotal;
    JLabel LAnswer, Reference, Mistakes;
    JLabel Ltime;
    JPanel p1, p2;

    JTextField operation[] = new JTextField[5];
    JTextField user_answer[] = new JTextField[5];
    JTextField[] ComputerAnswer = new JTextField[5];

    JTextField CorrectNumber;
    JTextField WrongNumber;
    JTextField TotalScore;

    JButton begin;
    JButton finish;
    float[] answer = new float[5];// counted,score,answer,qustion.
    String[] qustion = new String[5];
    String[] GainAnswer = new String[5];
    int number = 0;
    int Right = 0;
    int Wrong = 0;
    float Score = 0;
    int times=0;

    int time=0;
    int maxTime =120;
    Thread r=new Thread(this);
    JLabel useTime;
    
    JRadioButtonMenuItem items[];    
    JPopupMenu popupMenu;
    Color colorValues[] = { Color.pink, Color.green, Color.yellow, Color.gray,
            Color.blue, Color.CYAN };


    public MyJFrame(String[] qustion, float[] answer, int number) {
        this.answer = answer;
        this.qustion = qustion;
        this.number = number;
        // GainAnswer [] is an temp to compare user's answer and computer's
        // answer.

        this.setTitle("The four operations");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 450);
        this.setResizable(false);

        // Center setting
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screen = toolkit.getScreenSize();
        int x = (screen.width - this.getWidth()) / 2;
        int y = (screen.height - this.getHeight()) / 2;
        this.setLocation(x, y);

        p1 = new JPanel();
        p1.setLayout(null);
        p1.setBounds(2, 100, 100, 200);
        p1.setOpaque(false);
        
        Exercise1 = new JLabel("第一题:");
        Exercise1.setBounds(new Rectangle(10, 50, 80, 30));
        Exercise1.setForeground(Color.black);
        Exercise2 = new JLabel("第二题:");
        Exercise2.setBounds(new Rectangle(10, 100, 80, 30));
        Exercise2.setForeground(Color.black);
        Exercise3 = new JLabel("第三题:");
        Exercise3.setBounds(new Rectangle(10, 150, 80, 30));
        Exercise3.setForeground(Color.black);
        Exercise4 = new JLabel("第四题:");
        Exercise4.setBounds(new Rectangle(10, 200, 80, 30));
        Exercise4.setForeground(Color.black);
        Exercise5 = new JLabel("第五题:");
        Exercise5.setBounds(new Rectangle(10, 250, 80, 30));
        Exercise5.setForeground(Color.black);
        Font set = new Font("楷体", Font.BOLD, 15);
        Exercise1.setFont(set);
        Exercise2.setFont(set);
        Exercise3.setFont(set);
        Exercise4.setFont(set);
        Exercise5.setFont(set);

        // the place show the operations
        operation[0] = new JTextField();
        operation[0].setBounds(new Rectangle(70, 50, 200, 30));
        operation[0].setBackground(Color.cyan);
        operation[1] = new JTextField();
        operation[1].setBounds(new Rectangle(70, 100, 200, 30));
        operation[1].setBackground(Color.cyan);
        operation[2] = new JTextField();
        operation[2].setBounds(new Rectangle(70, 150, 200, 30));
        operation[2].setBackground(Color.cyan);
        operation[3] = new JTextField();
        operation[3].setBounds(new Rectangle(70, 200, 200, 30));
        operation[3].setBackground(Color.cyan);
        operation[4] = new JTextField();
        operation[4].setBounds(new Rectangle(70, 250, 200, 30));
        operation[4].setBackground(Color.cyan);
        // user_answer's place
        user_answer[0] = new JTextField();
        user_answer[0].setBounds(new Rectangle(280, 50, 100, 30));
        user_answer[1] = new JTextField();
        user_answer[1].setBounds(new Rectangle(280, 100, 100, 30));
        user_answer[2] = new JTextField();
        user_answer[2].setBounds(new Rectangle(280, 150, 100, 30));
        user_answer[3] = new JTextField();
        user_answer[3].setBounds(new Rectangle(280, 200, 100, 30));
        user_answer[4] = new JTextField();
        user_answer[4].setBounds(new Rectangle(280, 250, 100, 30));
        user_answer[0].setBackground(Color.cyan);
        user_answer[1].setBackground(Color.cyan);
        user_answer[2].setBackground(Color.cyan);
        user_answer[3].setBackground(Color.cyan);
        user_answer[4].setBackground(Color.cyan);

        if (number >= 1)
            user_answer[0].addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e) {
                    user_answer[0].setText(null);
                }

                public void focusLost(FocusEvent e) {
                }
            });
        if (number >= 2)
            user_answer[1].addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e) {
                    user_answer[1].setText(null);
                }

                public void focusLost(FocusEvent e) {
                }
            });
        if (number >= 3)
            user_answer[2].addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e) {
                    user_answer[2].setText(null);
                }

                public void focusLost(FocusEvent e) {
                }
            });
        if (number >= 4)
            user_answer[3].addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e) {
                    user_answer[3].setText(null);
                }

                public void focusLost(FocusEvent e) {
                }
            });
        if (number == 5)
            user_answer[4].addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e) {
                    user_answer[4].setText(null);
                }

                public void focusLost(FocusEvent e) {
                }
            });

        // the correct answers's area
        ComputerAnswer[0] = new JTextField();
        ComputerAnswer[0].setBounds(new Rectangle(380, 50, 100, 30));
        ComputerAnswer[1] = new JTextField();
        ComputerAnswer[1].setBounds(new Rectangle(380, 100, 100, 30));
        ComputerAnswer[2] = new JTextField();
        ComputerAnswer[2].setBounds(new Rectangle(380, 150, 100, 30));
        ComputerAnswer[3] = new JTextField();
        ComputerAnswer[3].setBounds(new Rectangle(380, 200, 100, 30));
        ComputerAnswer[4] = new JTextField();
        ComputerAnswer[4].setBounds(new Rectangle(380, 250, 100, 30));
        ComputerAnswer[0].setBackground(Color.cyan);
        ComputerAnswer[1].setBackground(Color.cyan);
        ComputerAnswer[2].setBackground(Color.cyan);
        ComputerAnswer[3].setBackground(Color.cyan);
        ComputerAnswer[4].setBackground(Color.cyan);

        LAnswer = new JLabel("答案");
        LAnswer.setBounds(new Rectangle(280, 20, 50, 30));
        LAnswer.setForeground(Color.black);
        
        LAnswer.setFont(set);

        Reference = new JLabel("参考答案");
        Reference.setBounds(new Rectangle(380, 20, 80, 30));
        Reference.setForeground(Color.black);
        Reference.setFont(set);
        
        Mistakes = new JLabel("错题:");
        Mistakes.setBounds(new Rectangle(10, 300, 60, 30));
        Mistakes.setForeground(Color.black);
        Mistakes.setFont(set);
        
        LCorrect = new JLabel("对题:");
        LCorrect.setBounds(new Rectangle(160, 300, 60, 30));
        LCorrect.setForeground(Color.black);
        LCorrect.setFont(set);
        
        LTotal = new JLabel("总分:");
        LTotal.setBounds(new Rectangle(300, 300, 60, 30));
        LTotal.setForeground(Color.black);
        LTotal.setFont(set);
        // button
        begin = new JButton("Start/Restart");
        begin.setBounds(new Rectangle(100,350,110,30));
        begin.setForeground(Color.black);
        begin.addActionListener(this);
        begin.setToolTipText("Begin or Restart your operations.");
        
        finish = new JButton("Finish");
        finish.setBounds(new Rectangle(280, 350, 100, 30));
        finish.setForeground(Color.black);
        finish.addActionListener(this);
        finish.setToolTipText("Check your answer.");
        
        WrongNumber = new JTextField();
        WrongNumber.setBounds(new Rectangle(50, 300, 80, 30));
        WrongNumber.setBackground(Color.red);
        
        CorrectNumber = new JTextField();
        CorrectNumber.setBounds(new Rectangle(200, 300, 80, 30));
        CorrectNumber.setBackground(Color.cyan);
        
        TotalScore = new JTextField();
        TotalScore.setBounds(new Rectangle(340, 300, 80, 30));
        TotalScore.setBackground(Color.green);
        
        Font set2 = new Font("楷体", Font.BOLD, 20);
        Ltime = new JLabel(" 右键更换背景颜色 ");        
        Ltime.setFont(set2);
        Ltime.setBounds(new Rectangle(10,10, 240, 20));
        Ltime.setForeground(Color.black);
        
        for (int i = 0; i < 5; i++) {
            operation[i].setText("NULL");
            user_answer[i].setText("Fill");
            ComputerAnswer[i].setText("Wait");
        }
        
        WrongNumber.setText(" NOT KNOW");
        CorrectNumber.setText(" NOT KNOW");
        TotalScore.setText(" NOT KNOW");
        
        useTime = new JLabel("时间");
        useTime.setBounds(new Rectangle(490, 10, 100, 20));
        useTime.setForeground(Color.red);
        useTime.setFont(set2);
        
        p1.add(Exercise1);
        p1.add(Exercise2);
        p1.add(Exercise3);
        p1.add(Exercise4);
        p1.add(Exercise5);
        
        p1.add(operation[0]);
        p1.add(operation[1]);
        p1.add(operation[2]);
        p1.add(operation[3]);
        p1.add(operation[4]);
        
        p1.add(user_answer[1]);
        p1.add(user_answer[2]);
        p1.add(user_answer[3]);
        p1.add(user_answer[4]);
        p1.add(user_answer[0]);
        
        p1.add(ComputerAnswer[0]);
        p1.add(ComputerAnswer[1]);
        p1.add(ComputerAnswer[2]);
        p1.add(ComputerAnswer[3]);
        p1.add(ComputerAnswer[4]);
        
        p1.add(LTotal);
        p1.add(TotalScore);
        p1.add(LCorrect);
        p1.add(CorrectNumber);
        p1.add(begin);
        p1.add(finish);
        p1.add(Mistakes);
        p1.add(WrongNumber);
        p1.add(Reference);
        p1.add(LAnswer);
        p1.add(Ltime);
        
        
        p1.add(useTime);
        this.add(p1);
        this.setVisible(true);
        
        r.start();
        r.suspend();//挂起
        
        
        // setting background colors
        popupMenu = new JPopupMenu();
        ButtonGroup colorGroup = new ButtonGroup();
        ItemHandler handler = new ItemHandler();    
        
        String colors[] = { "pink", "green", "yellow", "gray", "bule", "CYAN" };
        items = new JRadioButtonMenuItem[colors.length];
        // 创建弹出式菜单中的各个菜单项,并给每一个菜单项注册事件监听器
        
        for (int count = 0; count < items.length; count++) {
            items[count] = new JRadioButtonMenuItem(colors[count]);
            popupMenu.add(items[count]);
            colorGroup.add(items[count]);
            items[count].addActionListener(handler);
        }
        // background color
        getContentPane().setBackground(Color.white);
        this.addMouseListener(// 向主窗体注册监听器
        new MouseAdapter() {
            public void mousePressed(MouseEvent event) {
                checkForTriggerEvent(event);
            }

            public void mouseReleased(MouseEvent event) {
                checkForTriggerEvent(event);
            }

            // 检查MouseEvent事件,看其是否是弹出式菜单触发器
            private void checkForTriggerEvent(MouseEvent event) {
                if (event.isPopupTrigger()) {
                    popupMenu.show(event.getComponent(), event.getX(),
                            event.getY());
                }
            }
        });
    }

    // 菜单项选中时的事件处理
    private class ItemHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int i;
            for (i = 0; i < items.length; i++) {
                if (e.getSource() == items[i]) {
                    getContentPane().setBackground(colorValues[i]);
                }
            }
        }
    }
    
    public void run() {
        boolean stop=false;
        while(!stop){
            try{
                Thread.sleep(1000);
            }catch(InterruptedException ex){
            }
            time++;
            useTime.setText("用时"+String.valueOf(time)+"秒");
            if(maxTime==time){
                stop=true;
                JOptionPane.showMessageDialog(this, "超过答题时间,请停止答题!");
            }
        }
    }
    
    // ways
    public void actionPerformed(ActionEvent e) {
        int flag = 1;
        if (e.getSource() == begin) {
            r.resume();
            for (int i = 0; i < qustion.length; i++) {
                operation[i].setText(qustion[i]);
            }
            times++;
            if(times>1)
                {
                r.suspend();//挂起
                this.setVisible(false);    
                }
            if(times>1){
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            Input window = new Input();
                            window.frame.setVisible(true);
                            
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                
            }
        }

        if (e.getSource() == finish) {
            for (int j = 0; j < number; j++)
                GainAnswer[j] = user_answer[j].getText();

            for (int j = 0; j < number; j++)
                if (GainAnswer[j].isEmpty() || GainAnswer[j].equals("Fill")) {
                    flag = 0;
                    JOptionPane.showMessageDialog(this, "You didn't Finish !");
                    break;
                }
            
            if (flag == 1) {
                for (int x = 0; x < number; x++) {
                    if (answer[x] == changeanswer(GainAnswer[x])) {
                        Right++;
                        ComputerAnswer[x].setText("Right");
                    } else {
                        ComputerAnswer[x].setText(String.valueOf(answer[x]));
                    }
                }

                Wrong = (int) (((1.0 * Right) / number) * 10000);
                Score = Wrong / 100;
                TotalScore.setText(String.valueOf(Score));
                CorrectNumber.setText(String.valueOf(Right));
                WrongNumber.setText(String.valueOf(number - Right));    
                r.suspend();
            }
        }
    }

    // 是否是符号
    static int isoperator(char c) {
        if (c == '+')
            return 1;
        else if (c == '-')
            return 2;
        else if (c == '/')// 为了分数的答案做准备。
            return 3;
        return 0;
    }
    
    static float changeanswer(String a) {// 用户输入的答案
        float Ranswer = 0;
        int i;
        float temp[] = new float[2];
        String apart[] = new String[2];
        for (i = 0; i < a.length(); i++)
            if (isoperator(a.charAt(i)) == 3)
                break;
        if (i == a.length())
            Ranswer = Float.parseFloat(a);
        else {
            apart = a.split("/");
            temp[0] = Float.parseFloat(apart[0].toString());
            temp[1] = Float.parseFloat(apart[1].toString());
            Ranswer = temp[0] / temp[1];
            Ranswer = Math.round(Ranswer * 100);
            Ranswer = Ranswer / 100;
        }
        return Ranswer;
    }
}
用户做题

 

posted @ 2015-04-09 15:53  blackplume  阅读(362)  评论(3编辑  收藏  举报