结对编程:简易四则运算

 

功能:进行简易的四则运算,并根据给出的结果判断正误。

实现:使用java的图形化界面实现。

 

导入包库

1  package six;
2  import javax.swing.*;
3  import java.awt.*;
4  import java.awt.event.*;
5  import java.lang.*;

 

用于数字计算的Num类

 1 class Num{                              //定义数字类
 2     private int a;
 3     
 4     public Num()
 5     {
 6         this.a = (int)(Math.random()*10000);
 7     }
 8     
 9     public Num(int i)
10     {
11         this.a = (int)(Math.random()*4)+1;
12     }
13     
14     public int get_num()
15     {
16         return this.a;
17     }
18     
19     public int num_sum(Num B)
20     {
21         return this.a + B.a;
22     }
23     
24     public int num_dec(Num B)
25     {
26         return this.a - B.a;
27     }
28     
29     public int num_c(Num B)
30     {
31         return this.a * B.a;
32     }
33     
34     public int num_chu(Num B)
35     {
36         return this.a / B.a;
37     }
38     
39     public void flash()
40     {
41         this.a = (int)(Math.random()*100);
42     }
43     
44     public void flash_t()
45     {
46         this.a = (int)(Math.random()*4)+1;
47     }
48     
49     public boolean pd(int i)
50     {
51         if(this.a == i)
52             return true;
53         else
54             return false;
55     }
56 }

 

主类Main

public class Main {
    private static Num a = new Num();  //a,b
    private static Num b = new Num();
    private static Num c = new Num(1);
    private static int q = 0;
    private static String p;
    private static JFrame frame = new JFrame("简单四则运算");
    private static JLabel label1 = new JLabel("A");
    private static JLabel label2 = new JLabel("*");
    private static JLabel label3 = new JLabel("B");
    private static JLabel label4 = new JLabel(" "+"="+" ");
    private static JLabel label5 = new JLabel("结果");
    private static JButton button = new JButton("查看");
    private static JButton bt1 = new JButton("下一题");
    private static JButton bt2 = new JButton("退出");
    private static JTextField text = new JTextField(5);
    private static JPanel pane = new JPanel();  //容器
    
    public static void main(String[] args) {
        pane.setBorder(BorderFactory.createEmptyBorder(20,60,20,60));
        pane.setLayout(new GridLayout(0,5));  //设置布局
        pane.add(label1);
        pane.add(label2);
        pane.add(label3);
        pane.add(label4);
        pane.add(text);
        pane.add(label5);
        pane.add(button);
        pane.add(bt1);
        pane.add(bt2);

        if(c.pd(1)) 
        {
            p = "+";
            q = a.num_sum(b);
        }
        if(c.pd(2))
        {
            p = "-";
            q = a.num_dec(b);
        }
        if(c.pd(3))
        {
            p = "*";
            q = a.num_c(b);
        }
        if(c.pd(4))
        {
            p = "/";
            q = a.num_chu(b);
        }
         
        frame.getContentPane().add(pane,BorderLayout.CENTER);
        button.setMnemonic(KeyEvent.VK_I);
         
        button.addActionListener(new ActionListener(){  //鼠标单击事件
            public void actionPerformed(ActionEvent e){
                if(q == Integer.parseInt(text.getText().trim()))
                    label5.setText("正确!");
                else
                    label5.setText("错误!");
                
            }
        });
        
        bt1.setMnemonic(KeyEvent.VK_H);
        bt1.addActionListener(new ActionListener(){  //鼠标单击事件
            public void actionPerformed(ActionEvent e){
                a.flash();
                b.flash();
                c.flash_t();
                if(c.pd(1)) 
                {
                    p = "+";
                    q = a.num_sum(b);
                }
                if(c.pd(2))
                {
                    p = "-";
                    q = a.num_dec(b);
                }
                if(c.pd(3))
                {
                    p = "*";
                    q = a.num_c(b);
                }
                if(c.pd(4))
                {
                    p = "/";
                    q = a.num_chu(b);
                }
                label1.setText(" "+a.get_num()+" ");
                label2.setText(p);
                label3.setText(" "+b.get_num()+" ");
                text.setText(" ");
                label5.setText("结果");
            }
        });
        
        bt2.setMnemonic(KeyEvent.VK_T);
        bt2.addActionListener(new ActionListener(){  //鼠标单击事件
            public void actionPerformed(ActionEvent e){
                System.exit(0);
                
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        label1.setText(" "+a.get_num()+" ");
        label2.setText(p);
        label3.setText(" "+b.get_num()+" ");
        
        frame.setVisible(true);
    }
}

 

ssh://git@git.coding.net:amberpass/Calculate.git

 

https://git.coding.net/amberpass/Calculate.git

 

程序运行结果:

 

 

 

 

 

同组人:胡丽娜