作业5.1

主类:

package 四则运算;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.Timer;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;

import javax.swing.JTextField;
import javax.swing.JLabel;

public class jiemian extends JFrame implements ActionListener{
     /**
     * 
     */
    private static final long serialVersionUID = -2158244201317433166L;
    JTextField textField;
     JTextField textField_1;
     JTextField textField_2;
     JTextField textField_3;
     JTextField textField_4;
     JTextField textField_5;
     JTextField textField_6;
     JTextField textField_7;
     JTextField textField_8;
     JTextField textField_9;
     JTextField textField_10;
     JTextField textField_11;
     JTextField textField_12;
     JTextField t1;
     int a=0;
     JButton b1;
     JButton b2;
     JButton button;
     int[]daan=new int[10];
     private Timer tmr;
     JPanel []P = {new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             new JPanel(),
             };
     JTextField []T={new JTextField(20),
             new JTextField(20),
             new JTextField(20),
             new JTextField(20),
             new JTextField(20),
             new JTextField(20),
             new JTextField(20),
             new JTextField(20),
             new JTextField(20),
             new JTextField(20),
             new JTextField(10),
             new JTextField(10),
             new JTextField(10),
             new JTextField(10),
             new JTextField(10),
             new JTextField(10),
             new JTextField(10),
             new JTextField(10),
             new JTextField(10),
             new JTextField(10)};
    public jiemian() {
        JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.NORTH);
        b1 = new JButton("\u5F00\u59CB");
        b1.addActionListener(this);
        panel.setLayout(new GridLayout(0, 3, 0, 0));
        panel.add(b1);
        
        b2 = new JButton("\u4EA4\u5377");
        b2.addActionListener(this);
        panel.add(b2);
        
        button = new JButton("\u6362\u80A4");
        button.addActionListener(this);
        panel.add(button);
        
        JPanel panel_1 = new JPanel();
        getContentPane().add(panel_1, BorderLayout.SOUTH);
        panel_1.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_1.add(P[20]);
        
        JLabel label = new JLabel("\u7B54\u5BF9\u4E2A\u6570");
        P[20].add(label);
        
        textField_11 = new JTextField();
        P[20].add(textField_11);
        textField_11.setColumns(10);
        
        panel_1.add(P[21]);
        
        JLabel label_1 = new JLabel("\u7B54\u9519\u4E2A\u6570");
        P[21].add(label_1);
        
        textField_10 = new JTextField();
        P[21].add(textField_10);
        textField_10.setColumns(10);
        
        panel_1.add(P[22]);
        
        JLabel label_2 = new JLabel("\u603B\u5206");
        P[22].add(label_2);
        
        textField_12 = new JTextField();
        P[22].add(textField_12);
        textField_12.setColumns(10);
        
        panel_1.add(P[23]);
        
        JLabel label_3 = new JLabel("\u7528\u65F6");
        P[23].add(label_3);
        
        t1 = new JTextField("0");
        P[23].add(t1);
        t1.setColumns(10);
        
        JPanel panel_2 = new JPanel();
        getContentPane().add(panel_2, BorderLayout.CENTER);
        panel_2.setLayout(new GridLayout(10,3));
        
        JPanel panel_3 = new JPanel();
        panel_2.add(panel_3);
        panel_3.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_3.add(P[0]);
    
        P[0].add(T[0]);
        
        panel_3.add(P[1]);
        

        P[1].add(T[10]);
        
        JPanel panel_4 = new JPanel();
        panel_2.add(panel_4);
        panel_4.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_4.add(P[2]);
        
        P[2].add(T[1]);
        
        panel_4.add(P[3]);
        
        P[3].add(T[11]);
        
        JPanel panel_5 = new JPanel();
        panel_2.add(panel_5);
        panel_5.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_5.add(P[4]);
        
        P[4].add(T[2]);
        
        panel_5.add(P[5]);
        
        P[5].add(T[12]);
        
        JPanel panel_6 = new JPanel();
        panel_2.add(panel_6);
        panel_6.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_6.add(P[6]);
        
        P[6].add(T[3]);
        
        panel_6.add(P[7]);
        
        P[7].add(T[13]);
        
        JPanel panel_7 = new JPanel();
        panel_2.add(panel_7);
        panel_7.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_7.add(P[8]);
        
        P[8].add(T[4]);
        
        panel_7.add(P[9]);
        
        P[9].add(T[14]);
        
        JPanel panel_8 = new JPanel();
        panel_2.add(panel_8);
        panel_8.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_8.add(P[10]);
        
        P[10].add(T[5]);
        
        panel_8.add(P[11]);
        
        P[11].add(T[15]);
        
        JPanel panel_9 = new JPanel();
        panel_2.add(panel_9);
        panel_9.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_9.add(P[12]);
        
        P[12].add(T[6]);
        
        panel_9.add(P[13]);
        
        P[13].add(T[16]);
        
        JPanel panel_10 = new JPanel();
        panel_2.add(panel_10);
        panel_10.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_10.add(P[14]);
        
        P[14].add(T[7]);
        
        panel_10.add(P[15]);
        
        P[15].add(T[17]);
        
        JPanel panel_11 = new JPanel();
        panel_2.add(panel_11);
        panel_11.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_11.add(P[16]);
        
        P[16].add(T[8]);
        
        panel_11.add(P[17]);
        
        P[17].add(T[18]);
        
        JPanel panel_12 = new JPanel();
        panel_2.add(panel_12);
        panel_12.setLayout(new GridLayout(0, 2, 0, 0));
        
        panel_12.add(P[18]);
        
        P[18].add(T[9]);
        
        panel_12.add(P[19]);
        
        P[19].add(T[19]);
        Timer tmr = new Timer(1000, this);
        this.tmr = tmr;
        setSize(500,500);
        setLocationRelativeTo(null);
        setVisible(true);
        
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button)
        {
            if(a == 0)
            {
                for(int i=0;i<24;i++)
                {
                    P[i].setBackground(Color.GRAY);
                }
                a++;
            }
            else if (a == 1) 
            {
                for(int i=0;i<24;i++)
                {
                    P[i].setBackground(Color.orange);
                    
                }
                a++;
            }
            else if (a == 2) {
                for(int i=0;i<24;i++)
                {
                    P[i].setBackground(Color.pink);
                    
                }
                a=0;
            }
        }
        if (e.getSource() == b1)
        {
            int i=0;
            do
            {                
                int a,d,d1,d2;
                d1=(int)(1+Math.random()*(100-1+1));
                d2=(int)(1+Math.random()*(100-1+1));
                a=(int)(Math.random()*5);
                d=(int)(Math.random()*12+1);
                Suibian h=new Suibian();
                T[i].setText(h.toString(d1,d2,a,d));
                daan[i]=h.jisuan(d1,d2,a,d);
                i++;
            }while(i<10);
            t1.setText("0");
            tmr.start();
        } else {
            int t;
            t = Integer.parseInt(t1.getText());
            t++;
            t1.setText("" + t);

            if (t >= 120) {
                JFrame f=new JFrame();
                JOptionPane.showMessageDialog(f,"时间到,请交卷");
                tmr.stop();
            }
        }
        
        
        if (e.getSource() == b2)
        {
            int n=0;
            for(int k=0;k<20;k++)
            {
                try
                {
                if(T[k+10].getText().length()==0)
                {
                    continue;
                }
                if(daan[k]==Integer.parseInt(T[k+10].getText()))
                {
                    n++;
                }
                }
                catch(ArrayIndexOutOfBoundsException x)
                {
                    
                }
            }
            textField_11.setText(""+n);
            textField_10.setText(""+(10-n));
            textField_12.setText(""+(10*n));
            JFrame f=new JFrame();
            JOptionPane.showMessageDialog(f,""+daan[0]+"\n"+daan[1]+"\n"+daan[2]+"\n"+daan[3]+"\n"+daan[4]+"\n"+daan[5]+"\n"+daan[6]+"\n"+daan[7]+"\n"+daan[8]+"\n"+daan[9]);
            tmr.stop();
            }
    }
    
    
    public static void main(String[] args) {
        new jiemian();
    }
}

核心类:

package 四则运算;

public class Suibian {
    public String toString(int d1,int d2,int a,int d)
    {
        if(a==0)
        {
            return d1+"+"+d2+"=";
        }
        else if(a==1)
        {
            return d1+"-"+d2+"=";
        }
        else if(a==2)
        {
            return d1+"×"+d2+"=";
        }
        else if(a==3)
        {
            if(d2==0)
                d2=d2+1;
            return d1+"÷"+d2+"=";
        } 
        else
        {
            return d+"!";
        }
    }
    
    public int jisuan(int d1,int d2,int a,int d)
    {
        if(a==0)
        {
            return d1+d2;
        }
        else if(a==1)
        {
            return d1-d2;
        }
        else if(a==2)
        {
            return d1*d2;
        }
        else if(a==3)
        {
            if(d2==0)
                d2=d2+1;
            return d1/d2;
        } 
        else
        {
            return jiecheng(d);
        }
    }
    
    private int jiecheng(int x) 
    {
        if(x==0 || x==1)
        {
            return 1;
        }
        else
        {
            return x*jiecheng(x-1);
        }
    }
}

 

posted @ 2015-04-30 18:27  04吴旭涛  阅读(106)  评论(2编辑  收藏  举报