• 结组开发


    1、第一阶段目标:重构四则运算-- 能把计算的功能封装起来,通过API 接口调用计算方法。

    定义一个计算核心类:把四则运算的计算功能包装在一个模块中 (这个模块可以是一个类 Class,  一个DLL等等)。

    2、 第二阶段目标 - 通过测试程序和API 接口测试其简单的加减乘除功能。并能看到代码覆盖率。

    3、第三阶段目标 – 定义异常处理。

    如果输入是有错误的,例如 “1 ++ 2”, 在数值范围是 -1000 .. 1000 的时候,传进去 “10000 + 32768 * 3”,  或者是 “ 248.04 / 0”  怎么办? 怎么告诉函数的调用者 “你错了”?  把返回的字符串定义为 “-1” 来表示? 那么如果真的计算结果是 “-1” 又怎么处理呢?建议这个时候,要定义各种异常 (Exception), 让 Core 在碰到各种异常情况的时候,能告诉调用者 - 你错了!

    代码:

    frame.java:

    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 szys.frame;
    
    import java.awt.EventQueue;
    
    public class frame {
    
        private JFrame frame;
        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() * 3);
        
    
        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.
         */
        private void initialize() throws Yichang{
            frame = new JFrame();
            frame.setBounds(100, 100, 517, 352);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JPanel panel = new JPanel();
            frame.getContentPane().add(panel, BorderLayout.CENTER);
            panel.setLayout(null);
            
            JLabel lblNewLabel = new JLabel("u56DBu5219u8FD0u7B97u56685.2");
            lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            lblNewLabel.setBounds(141, 0, 208, 57);
            panel.add(lblNewLabel);
            
            textField = new JTextField();
            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.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_2 = new JTextField("=");
            textField_2.setHorizontalAlignment(SwingConstants.CENTER);
            textField_2.setEditable(false);
            textField_2.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            textField_2.setColumns(10);
            textField_2.setBounds(318, 82, 36, 45);
            panel.add(textField_2);
            
            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 ");
            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());    
                    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();
                        }*/
    
                    }
            });
        }
    }

    input.java:

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

    yichang.java:

    class Yichang extends Exception 
    {  
        public Yichang(String msg)      
        {  
            super(msg);  
        }  
    }  

    Expression.java:

    import java.util.*;
    
    import szys.Input;
    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 expressionadd()
    {
        answer = a+b;
        return answer;
    }
    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();
        
        //answer = expression.answer;
        //Input input = new Input();
        //Expression.c = input.a;
        /*try{
            Expression.c = in.nextInt();
        }
        catch(InputMismatchException e)
        {   System.err.println("
    错误!  ,请你输入一个整数");
        }*/
        //if(answer==c)
    /*    {
            System.out.print("答对了");
        }
        else System.out.print("答错了");*/
        //System.out.print("answer="+answer);
    }
    }

    运行截图:

     

  • 相关阅读:
    SecureCRT安装
    wmv12下安装centos7
    SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute
    oracle之 redo过高诊断
    oracle之 手动创建 emp 表 与 dept 表
    oracle之 11.2.0.4 bbed安装
    oracle12c之 控制pdb中sga 与 pga 内存使用
    storm之 Storm 工作原理
    spark之 spark 2.2.0 Standalone安装、wordCount演示
    hadoop之 参数调优
  • 原文地址:https://www.cnblogs.com/xjmm/p/13044479.html
Copyright © 2020-2023  润新知