5.2-5.3

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) {
         int a1=0;
         int b1=0;
         int cc=0;
         int dd=0;
        new jiemian();
        Suibian q=new Suibian();
        q.input();
        a1=q.aa;
        b1=q.bb;
        cc=q.cc;
        dd=q.dd;
        
         int total=q.jisuan(a1,b1,cc,dd);
        System.out.println("计算结果是:"+total);        
        
    }
}
主类
package 四则运算;

import java.util.Scanner;

public class Suibian {
    int aa,bb,cc,dd;
     static Scanner sc = new Scanner(System.in);
    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);
        }
    }
    public void input()
    {
        
        try{
        System.out.println("运算符是 +(0),-(1),*(2),/(3)"); 
        
        System.out.println("请输入第一个数:");
         aa=sc.nextInt();
        System.out.println("请输入第二个数:");
         bb=sc.nextInt();
        System.out.println("请输入运算符:");
         cc=sc.nextInt();
         
        }catch(Exception  e)
    {
        System.out.println("输入数值出现异常");
    }
    }
}
封装类
package 四则运算;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class Testss {
    @Test
    public void testAdd()// 测试加法基本功能
    {

        int result = new Suibian().jisuan(2, 2, 0, 1);
        assertEquals(4, result);
    }

    @Test
    public void testSub()// 测试减法基本功能
    {

        int result = new Suibian().jisuan(2, 2, 1, 1);
        assertEquals(0, result);
    }

    @Test
    public void testMuti()// 测试乘法基本功能
    {

        int result = new Suibian().jisuan(3, 2, 2, 1);
        assertEquals(6, result);
    }

    @Test
    public void testDivi()// 测试除法基本功能
    {

        int result = new Suibian().jisuan(6, 2, 3, 1);
        assertEquals(3, result);
    }

    @Test
    public void testDiviZero()// 测试除法出书不为零基本功能
    {

        int result = new Suibian().jisuan(3, 0, 3, 2);
        assertEquals(3, result);
    }

    @Test
    public void testIlligal()// rrrrrrr
    {
        Suibian q = new Suibian();
        q.input();

    }
}
测试类

测试结果

测试结果分析:

在单元测试中写入我们所要测试的测试代码,并给出测试用例,在所给测试用例的情况下,如果预期结果与实际结果相一致(即测试情况均为绿色打钩通过),则代表测试结果通过,否则则表示程序有错。

白盒测试:

编号 数据(a,b,c,d) 预期 实际 状态
1 (2,2,0,1) 4 4 正确
2 (2,2,1,1) 0 0 正确
3  (3,2,2,1) 6 6 正确
4  (6,2,3,1) 3 3 正确
5  (3,0,3,2) 3 3 正确

黑盒测试:

当输入字符的时候会报错

 

 

出现的程序问题及解决:

标注@Test不能删掉,当时删除了研究了很久才发现不能删除标注

AssertEquals()支持boolean,long,int等类型变量

posted on 2015-05-08 18:43  05莫俊余  阅读(137)  评论(1编辑  收藏  举报

导航