zoukankan      html  css  js  c++  java
  • 作业5.2 测试与封装

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


    总结:这次和队友曾柏树(http://www.cnblogs.com/cypress-zeng/)改进的程序,比上次都有所进步,望老师批改。
  • 相关阅读:
    <转>MSDN上关于XPath的语法文章
    <转>正则表达式语法
    <转>反射技术的简单介绍
    <转>css中用expression实现js的onmouseover/onmouseout事件
    <转>在xslt中实现split方法对查询字符串进行分隔
    <转>SQL Server中的XML数据进行insert、update、delete
    <转>VS2010项目转换到VS2008下方法
    <转>在xslt 1.0 中取得当前时间
    C# 发送邮件的Helper类
    DataTable 类的学习笔记
  • 原文地址:https://www.cnblogs.com/zhangqicong/p/4488219.html
Copyright © 2011-2022 走看看