• 作业五 四则元算 测试与封装


    未封装的程序:

    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    public class Size {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            myframe f=new myframe();
        }
    
    }
    class myframe extends JFrame
    {
        JLabel L1=new JLabel("题目:");
        JLabel L2=new JLabel("输入题目个数:");
        JLabel L3=new JLabel("提示:");
        JLabel L4=new JLabel("        ");   //题目
        JLabel L5=new JLabel("题目个数不能大于5");   //提示
        JTextField T1=new JTextField(6);
        JButton B1=new JButton("开始");
        JButton B2=new JButton("确认");
        JButton B3=new JButton("下一题");
        JButton B4=new JButton("换皮肤");
        JPanel p1=new JPanel();
        int n=0,a,b,c,d,j,k,l,m=0,p;
        double result;
        char[]h={'+','-','*','/'},h1={'(',')','!'};
        String s1,s2;
        Color col[]={Color.blue,Color.green,Color.ORANGE,Color.PINK,Color.GRAY,Color.RED,Color.LIGHT_GRAY};
        myframe(){
            this.setTitle("四则运算");
            this.setSize(300,280);
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.add(p1); 
            p1.setLayout(null);
            L1.setBounds(50, 20, 90, 30);
            p1.add(L1);
            L4.setBounds(140, 20, 150, 30);
            p1.add(L4);
            L2.setBounds(50, 70, 90, 30);
            p1.add(L2);
            T1.setBounds(150, 70, 90, 30);
            p1.add(T1);
            B4.setBounds(100, 110, 90, 30);
            p1.add(B4);
            B1.setBounds(10, 150, 90, 30);
            p1.add(B1);
            B2.setBounds(100, 150, 90, 30);
            p1.add(B2);
            B2.setEnabled(false);
            B3.setBounds(190, 150,90,30);
            p1.add(B3);
            L3.setBounds(50, 200, 50, 30);
            B3.setEnabled(false);
            p1.add(L3);
            L5.setBounds(100, 200, 180, 30);
            p1.add(L5);
            this.setVisible(true);
            this.setLocationRelativeTo(null);
            B1.addActionListener(new B());
            B2.addActionListener(new B());
            B3.addActionListener(new B());
            B4.addActionListener(new B());
        }
        class B implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if(e.getSource()==B1)
                {
                    p=n=(int) Double.parseDouble(T1.getText());
                    //System.out.print(n);
                    if(n>5||n<1)
                    {
                        JOptionPane.showMessageDialog(null,"输入的个数不能大于5或者小于1","注意",JOptionPane.INFORMATION_MESSAGE);
                    }
                    else
                    {
                        B1.setEnabled(false);
                        B2.setEnabled(true);
                        B3.setEnabled(true);
                        L2.setText("输入答案 :");
                        L5.setText(null);
                        T1.setText(null);
                        star();
                    }
                }
                if(e.getSource()==B2)
                {
                    l=(int) Double.parseDouble(T1.getText());
                    if(l==result)
                    {
                        m++;
                        s2="答案正确!";
                        L5.setText(s2);
                    }
                    else{
                        s2="答案错误!答案为:"+String.valueOf(result);
                        L5.setText(s2);
                    }
                    B2.setEnabled(false);
                }
                if(e.getSource()==B3)
                {
                    if(n==0)
                    {
                        JOptionPane.showMessageDialog(null, "一共"+p+"道题目。正确的题数为:"+m,"成绩",JOptionPane.INFORMATION_MESSAGE);
                        B1.setEnabled(true);
                        B2.setEnabled(false);
                        B3.setEnabled(false);
                        L2.setText("输入题目个数:");
                        L4.setText(null);
                        T1.setText(null);
                    }
                    else
                    {
                        L5.setText(null);
                        T1.setText(null);
                        star();
                        B2.setEnabled(true);
                    }
                }
                if(e.getSource()==B4)
                {
                    int i;
                    i=(int)(Math.random()*6);
                    p1.setBackground(col[i]);
                }
            }
        }
        void star()
        {
            a=(int) (Math.random()*10);
            b=(int) (Math.random()*10);
            c=(int) (Math.random()*10);
            d=(int) (Math.random()*10);
            j=(int) (Math.random()*4);
            k=(int) (Math.random()*4);
            if(d<0)
            {
                a=(int) (Math.random()*9+1);
                s1=String.valueOf(a)+String.valueOf(h1[2]);
                L4.setText(s1);
                result=1;
                for(int i=1;i<=a;i++)
                {
                    result=result*i;
                }
            }
            else
            {
                if(b>=0&&c>=0)
                {
                    s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(b)+String.valueOf(h[k])+String.valueOf(c);
                    L4.setText(s1);
                    System.out.println(s1);    
                }
                else if(b<0&&c>=0)
                {
                    s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(h1[0])+String.valueOf(b)+String.valueOf(h1[1])+String.valueOf(h[k])+String.valueOf(c);
                    L4.setText(s1);
                    System.out.println(s1);    
                }
                else if(b>=0&&c<0)
                {
                    s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(b)+String.valueOf(h[k])+String.valueOf(h1[0])+String.valueOf(c)+String.valueOf(h1[1]);
                    L4.setText(s1);
                    System.out.println(s1);    
                }
                else
                {
                    s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(h1[0])+String.valueOf(b)+String.valueOf(h1[1])+String.valueOf(h[k])+String.valueOf(h1[0])+String.valueOf(c)+String.valueOf(h1[1]);
                    L4.setText(s1);
                    System.out.println(s1);
                }
                if(k>1)  //后面的计算符为乘除,先算后面两个数
                {
                    if(k==2)
                    {
                        result=b*c;    
                    }
                    else if(k==3)
                    {
                        result=b/c;
                    }
                    if(j==0)
                    {
                        result=result+a;
                    }
                    else if(j==1)
                    {
                        result=a-result;
                    }
                    else if(j==2)
                    {
                        result=a*result;
                    }
                    else if(j==3)
                    {
                        result=a/result;
                    }
                }
                else if(k<2)
                {
                    if(j==0)
                    {
                        result=b+a;
                    }
                    else if(j==1)
                    {
                        result=a-b;
                    }
                    else if(j==2)
                    {
                        result=a*b;
                    }
                    else if(j==3)
                    {
                        result=a/b;
                    }
                    if(k==0)
                        result=result+c;
                    if(k==1)
                        result=result-c;
                }
            }
            System.out.println(result);    
            n--;
        }
    }

    封装后的程序:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    public class Size {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            myframe f=new myframe();
        }
    
    }
    class myframe extends JFrame
    {
        JLabel L1=new JLabel("题目:");
        JLabel L2=new JLabel("输入题目个数:");
        JLabel L3=new JLabel("提示:");
        JLabel L4=new JLabel("        ");   //题目
        JLabel L5=new JLabel("题目个数不能大于5");   //提示
        JTextField T1=new JTextField(6);
        JButton B1=new JButton("开始");
        JButton B2=new JButton("确认");
        JButton B3=new JButton("下一题");
        JButton B4=new JButton("换皮肤");
        JPanel p1=new JPanel();
        int n=0,a,b,c,d,j,k,l,m=0,p;
        double result;
        char[]h={'+','-','*','/'},h1={'(',')','!'};
        String s1,s2;
        Color col[]={Color.blue,Color.green,Color.ORANGE,Color.PINK,Color.GRAY,Color.RED,Color.LIGHT_GRAY};
        myframe(){
            this.setTitle("四则运算");
            this.setSize(300,280);
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.add(p1); 
            p1.setLayout(null);
            L1.setBounds(50, 20, 90, 30);
            p1.add(L1);
            L4.setBounds(140, 20, 150, 30);
            p1.add(L4);
            L2.setBounds(50, 70, 90, 30);
            p1.add(L2);
            T1.setBounds(150, 70, 90, 30);
            p1.add(T1);
            B4.setBounds(100, 110, 90, 30);
            p1.add(B4);
            B1.setBounds(10, 150, 90, 30);
            p1.add(B1);
            B2.setBounds(100, 150, 90, 30);
            p1.add(B2);
            B2.setEnabled(false);
            B3.setBounds(190, 150,90,30);
            p1.add(B3);
            L3.setBounds(50, 200, 50, 30);
            B3.setEnabled(false);
            p1.add(L3);
            L5.setBounds(100, 200, 180, 30);
            p1.add(L5);
            this.setVisible(true);
            this.setLocationRelativeTo(null);
            B1.addActionListener(new B());
            B2.addActionListener(new B());
            B3.addActionListener(new B());
            B4.addActionListener(new B());
        }
        class B implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if(e.getSource()==B1)
                {
                   p=n=(int) Double.parseDouble(T1.getText());
                    //System.out.print(n);
                    if(n>5||n<1)
                    {
                        JOptionPane.showMessageDialog(null,"输入的个数不能大于5或者小于1","注意",JOptionPane.INFORMATION_MESSAGE);
                    }
                    else
                    {
                        B1.setEnabled(false);
                        B2.setEnabled(true);
                        B3.setEnabled(true);
                        L2.setText("输入答案 :");
                        L5.setText(null);
                        T1.setText(null);
                        Star.star(h1, h,a,b,c,d,n,j,k,s1,L4,result);
                    }
                }
                if(e.getSource()==B2)
                {
                    l=(int) Double.parseDouble(T1.getText());
                    if(l==result)
                    {
                        m++;
                        s2="答案正确!";
                        L5.setText(s2);
                    }
                    else{
                        s2="答案错误!答案为:"+String.valueOf(result);
                        L5.setText(s2);
                    }
                    B2.setEnabled(false);
                }
                if(e.getSource()==B3)
                {
                    if(n==0)
                    {
                        JOptionPane.showMessageDialog(null, "一共"+p+"道题目。正确的题数为:"+m,"成绩",JOptionPane.INFORMATION_MESSAGE);
                        B1.setEnabled(true);
                        B2.setEnabled(false);
                        B3.setEnabled(false);
                        L2.setText("输入题目个数:");
                        L4.setText(null);
                        T1.setText(null);
                    }
                    else
                    {
                        L5.setText(null);
                        T1.setText(null);
                        Star.star(h1, h,a,b,c,d,n,j,k,s1,L4,result);
                        B2.setEnabled(true);
                    }
                }
                if(e.getSource()==B4)
                {
                    int i;
                    i=(int)(Math.random()*6);
                    p1.setBackground(col[i]);
                }
            }
        }
       
    }
    import javax.swing.JLabel;
    
    
    public class Star {
        public static void star(char[]h1,char[]h,int a,int b,int c,int d,int n,int j,int k,String s1,JLabel L4,double result){
                a=(int) (Math.random()*200-100);
                b=(int) (Math.random()*200-100);
                c=(int) (Math.random()*200-100);
                d=(int) (Math.random()*200-100);
                j=(int) (Math.random()*4);
                k=(int) (Math.random()*4);
                if(d<0)
                {
                    a=(int) (Math.random()*9+1);
                    s1=String.valueOf(a)+String.valueOf(h1[2]);
                    L4.setText(s1);
                    result=1;
                    for(int i=1;i<=a;i++)
                    {
                        result=result*i;
                    }
                }
                else
                {
                    if(b>=0&&c>=0)
                    {
                        s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(b)+String.valueOf(h[k])+String.valueOf(c);
                        L4.setText(s1);
                        System.out.println(s1);    
                    }
                    else if(b<0&&c>=0)
                    {
                        s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(h1[0])+String.valueOf(b)+String.valueOf(h1[1])+String.valueOf(h[k])+String.valueOf(c);
                        L4.setText(s1);
                        System.out.println(s1);    
                    }
                    else if(b>=0&&c<0)
                    {
                        s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(b)+String.valueOf(h[k])+String.valueOf(h1[0])+String.valueOf(c)+String.valueOf(h1[1]);
                        L4.setText(s1);
                        System.out.println(s1);    
                    }
                    else
                    {
                        s1=String.valueOf(a)+String.valueOf(h[j])+String.valueOf(h1[0])+String.valueOf(b)+String.valueOf(h1[1])+String.valueOf(h[k])+String.valueOf(h1[0])+String.valueOf(c)+String.valueOf(h1[1]);
                        L4.setText(s1);
                        System.out.println(s1);
                    }
                    if(k>1)  //后面的计算符为乘除,先算后面两个数
                    {
                        if(k==2)
                        {
                            result=b*c;    
                        }
                        else if(k==3)
                        {
                            result=b/c;
                        }
                        if(j==0)
                        {
                            result=result+a;
                        }
                        else if(j==1)
                        {
                            result=a-result;
                        }
                        else if(j==2)
                        {
                            result=a*result;
                        }
                        else if(j==3)
                        {
                            result=a/result;
                        }
                    }
                    else if(k<2)
                    {
                        if(j==0)
                        {
                            result=b+a;
                        }
                        else if(j==1)
                        {
                            result=a-b;
                        }
                        else if(j==2)
                        {
                            result=a*b;
                        }
                        else if(j==3)
                        {
                            result=a/b;
                        }
                        if(k==0)
                            result=result+c;
                        if(k==1)
                            result=result-c;
                    }
                }
                System.out.println(result);    
                n--;
            }
    }
    具体结果和总结在我的队友14曾柏书(http://www.cnblogs.com/cypress-zeng/)的博客里,那边会有详细地解释啦。
  • 相关阅读:
    程序员面试中有几大法则
    css 页面重绘和回流(重排)以及优化
    (一)Windows下安装RabbitMQ服务
    NOT IN 和 OR 的SQL语句优化
    java更改图片格式,,更改图片大小,并实现多线程上传图片。
    数据库根据坐标查找附近的店铺
    Spring MVC接收提交文件图片的两种方式
    搜索引擎选择: Elasticsearch与Solr
    redis
    浅谈分布式事务
  • 原文地址:https://www.cnblogs.com/zhangqicong/p/4468684.html
Copyright © 2020-2023  润新知