结对开发

团队:蒲顺利 张志流

package com.operation;

 

 

import java.util.*;

 

 

public class Expression

{

    private int a = (int)(Math.random() * Math.pow(10,2)-1);

    private int b = (int)(Math.random() * Math.pow(10,2)-1);

    private int op;

    static int c;

    int answer;

    int answer1;

    static Scanner in=new Scanner(System.in);

    

public int geta()

{

    return a;

}

 

public void seta(int a)

{

    this.a = a;

}

 

public int getb()

{

    return b;

}

 

public void setb(int b)

{

    this.b = b;

}

 

public int expressionmin()

{

    answer = a-b;

    return answer;

}

public int expressionchen()

{

    answer = a*b;

    return answer;

}

public int expressionchu()

{

    b = (b == 0 ? b = 1 +(int)(Math.random() * Math.pow(10,2)-1):b);//閹烘帡娅庨梽銈嗙《鐞氼偊娅庨弫棰佽礋0閻ㄥ嫭鍎忛崘锟�

    answer = a/b;

    return answer;

}

public static void main(String[] args){

    int answer;

    Expression expression = new Expression();

    

}

}

 

package com.operation;

 

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.Font;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

 

import com.operation.frame;

 

import java.awt.EventQueue;

 

public class frame {

 

    private JFrame frame;//图形界面,简单的window窗口

    private JTextField textField;//文本框

    private JTextField textField_1;

    private JTextField textField_2;

    private JTextField textField_3;

    private JTextField textField_4;

    private JTextField textField_5;

    frame f;

    

    int a;

    int b;

    char []op={'+','-','*','/'};

    String Sa;

    String Sb;

    int result;

    static int i = (int) (Math.random() * 4);//大于0小于4的随机数

    

 

    Expression expression = new Expression();

    private JButton btnNewButton_1;//按钮

 

 

    /**

     * Launch the application.

     */

    public static void main(String[] args)

    {

        EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {

                    frame window = new frame();

                    window.frame.setVisible(true);

                    System.out.print(i);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });

    }

 

    /**

     * Create the application.

     * @throws Yichang

     */

    public frame() throws Yichang {

        initialize();

    }

 

    /**

     * Initialize the contents of the frame.

     */

  

        JPanel panel = new JPanel();

        frame.getContentPane().add(panel, BorderLayout.CENTER);

        panel.setLayout(null);

        

        JLabel lblNewLabel = new JLabel("\u56DB\u5219\u8FD0\u7B97\u5668");//四则运算器

        lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));

        lblNewLabel.setBounds(141, 0, 208, 57);

        panel.add(lblNewLabel);

        

        textField = new JTextField();//用来放随机数的第一个

        textField.setEditable(false);

        textField.setHorizontalAlignment(SwingConstants.CENTER);//设置文本时是水平对齐方式

        textField.setFont(new Font("微软雅黑", Font.PLAIN, 30));

        textField.setBounds(56, 82, 88, 45);

        panel.add(textField);//将组件插入到面板

        textField.setColumns(10);

        

        textField_1 = new JTextField();//用来存放随机数的第二个

        textField_1.setEditable(false);

        textField_1.setHorizontalAlignment(SwingConstants.CENTER);

        textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));//设置字体格式

        textField_1.setColumns(10);//设置文本框长度

        textField_1.setBounds(220, 82, 88, 45);

        panel.add(textField_1);

       

        textField_3 = new JTextField();//写入自己的答案

        textField_3.setHorizontalAlignment(SwingConstants.CENTER);

        textField_3.setFont(new Font("微软雅黑", Font.PLAIN, 30));

        textField_3.setColumns(10);

        textField_3.setBounds(371, 82, 88, 45);

        panel.add(textField_3);

        

        JButton btnNewButton = new JButton("\u505A\u5B8C\u4E86");//做完了

        btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 30));

        btnNewButton.setBounds(188, 156, 131, 45);

        panel.add(btnNewButton);

        

        JLabel lblNewLabel_1 = new JLabel("\u8BA1\u7B97\u7ED3\u679C");//计算结果

        lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));

        lblNewLabel_1.setBounds(56, 222, 131, 35);

        panel.add(lblNewLabel_1);

        

        textField_4 = new JTextField();

        textField_4.setFont(new Font("微软雅黑", Font.PLAIN, 20));

        textField_4.setColumns(10);

        textField_4.setBounds(188, 222, 161, 35);

        panel.add(textField_4);

        

        textField_5 = new JTextField();//运算符

        textField_5.setHorizontalAlignment(SwingConstants.CENTER);

        textField_5.setEditable(false);

        textField_5.setFont(new Font("微软雅黑", Font.PLAIN, 30));

        textField_5.setText(String.valueOf(op[i]));

        textField_5.setColumns(10);

        textField_5.setBounds(159, 82, 44, 45);

        panel.add(textField_5);

        

        a = expression.geta();

        Sa = String.valueOf(a);

        textField.setText(Sa);

        

        b = expression.getb();

        Sb = String.valueOf(b);

        textField_1.setText(Sb);

        

        btnNewButton_1 = new JButton("\u518D\u6765 ");//u518D\u6765  再来的意思

        btnNewButton_1.addActionListener(new ActionListener() {//让按钮具备关闭窗口的功能

            public void actionPerformed(ActionEvent e) {

                

                try {

                        f = new frame();

                } catch (Yichang e1) {

                    // TODO 自动生成catch块

                    e1.printStackTrace();

                }

                frame.dispose();

                f.frame.setVisible(true);//显示窗体

                

            }

        });

        btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));

        btnNewButton_1.setBounds(366, 222, 107, 36);

        panel.add(btnNewButton_1);

        

 

        btnNewButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0){

                int n3=Integer.valueOf(textField_3.getText().toString()); // 获得textField中输入的值

                switch(op[i])

                {

                    case '+':result = expression.expressionadd();break;

                    case '-':result = expression.expressionmin();break;

                    case '*':result = expression.expressionchen();break;

                    case '/':result = expression.expressionchu();break;

                }

                if(textField_3.getText()!="")

                {

                     if(result==n3)

                        {

                            textField_4.setText("正确");

                        }

                         else

                        {

                            textField_4.setText("错误!答案为:"+result);

                        }

                }     

                try {

                    if(textField_3.getText()=="")

                    throw new Yichang("不能为空");

                    } catch (Yichang e) {

                        // TODO 自动生成的catch块

                        e.printStackTrace();

                    }

                

                /*try {

                 if(n3<0)

                

                        throw new Yichang("不可能是负数!");

                    } catch (Yichang e) {

                          // TODO 自动生成的catch块

                        e.printStackTrace();

                    }*/

 

                }

        });

    }

}

 

package com.operation;

 

import java.util.*;

 

public class Input {

    public int a;

    static Scanner in=new Scanner(System.in);

    public void seta(int intput){

        a = intput;

    }

    public Input()

    {

        try{

            a = in.nextInt();

        }

        catch(InputMismatchException e)

        {  

            System.err.println("杈撳叆");

        }

    }

    public int getinput()

    {

        return a;

    }

    

}

 

package com.operation;

 

 

class Yichang extends Exception

{  

    public Yichang(String msg)      

    {  

        super(msg);  

    }  

}  

上一篇:flutter 中如何获取输入文本框的内容


下一篇:实验十