• 结对开发


    团队:蒲顺利 张志流

    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("u56DBu5219u8FD0u7B97u5668");//四则运算器

            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("u505Au5B8Cu4E86");//做完了

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

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

            panel.add(btnNewButton);

            

            JLabel lblNewLabel_1 = new JLabel("u8BA1u7B97u7ED3u679C");//计算结果

            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("u518Du6765 ");//u518Du6765  再来的意思

            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);  

        }  

    }  

  • 相关阅读:
    兄弟连学python——MongoDB相关
    兄弟连学python——redis相关
    python学习——tkinter实战(猜价格)
    python学习——tkinter实战(计算器)
    python学习——while实战小练习
    python学习——循环结构
    python学习——数据类型转换
    python学习——通过while循环语句实现九九乘法表的四种表达方式
    python—Html表单
    python—Redis简介及应用
  • 原文地址:https://www.cnblogs.com/zl00/p/13088293.html
Copyright © 2020-2023  润新知