码迷,mamicode.com
首页 > 其他好文 > 详细

作业5.2

时间:2015-05-08 20:06:08      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:

主类:

package 四则运算;
import javax.swing.JFrame;

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;
	 float[]daan=new float[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)(Math.random()*201-100);
				d2=(int)(Math.random()*201-100);
				a=(int)(Math.random()*5);
				d=(int)(Math.random()*12);
				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 四则运算;

import java.text.DecimalFormat;

public class Suibian {
	public String toString(int d1,int d2,int a,int d)
	{
		String A=0+"";
		String B=0+"";
		if(d1>=0)
		{
			A=d1+"";
		}
		else if(d1<0)
		{
			A="("+d1+")";
		}
		if(d2>=0)
		{
			B=d2+"";
		}
		else if(d2<0)
		{
			B="("+d2+")";
		}
		if(a==0)
		{
			return A+"+"+B+"=";
	    }
		else if(a==1)
		{
			return A+"-"+B+"=";
		}
		else if(a==2)
		{
			return A+"×"+B+"=";
		}
		else if(a==3)
		{
			if(d2==0)
				d2=d2+1;
			return A+"÷"+B+"=";
		} 
		else
		{
			return d+"!";
		}
	}
	
	public float jisuan(int d1,int d2,int a,int d)
	{
		float x;
		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;
		    x=(float)d1/(float)d2;
		    DecimalFormat xs = new DecimalFormat("#.00");
		    x=Float.parseFloat(xs.format(x));
		    return x;
		} 
		else
		{
			return jiecheng(d);
		}
	}
	
	private int jiecheng(int x) 
	{
		if(x==0 || x==1)
		{
			return 1;
		}
		else
		{
			return x*jiecheng(x-1);
		}
	}
}

  测试类:

package 四则运算;

import org.junit.Test;

public class test {

	@Test
	public void testjia()//测试加法基本功能
    {
        
        float result=new Suibian().jisuan(3,3,0,3);
        if(result==6.0)
        	System.out.println("正确");
        else
        	System.out.println("错误");
    }

	@Test
	public void testjian()//测试减法基本功能
    {
        
        float result=new Suibian().jisuan(3,3,1,3);
        if(result==0.0)
        	System.out.println("正确");
        else
        	System.out.println("错误");
    }

	@Test
	public void testcheng()//测试乘法基本功能
    {
        
        float result=new Suibian().jisuan(3,3,2,3);
        if(result==9.0)
        	System.out.println("正确");
        else
        	System.out.println("错误");
    }
	@Test
	public void testchu()//测试除法基本功能
    {
        
        float result=new Suibian().jisuan(3,3,3,3);
        if(result==1.0)
        	System.out.println("正确");
        else
        	System.out.println("错误");
    }

}

  单元测试:

加减乘除:技术分享

 

黑盒测试:输入                  结果

                a                     出错

                !                    出错

      11111111111           出错

               空               默认答案错误

 

感想:这次的测试让我意识到自己程序的种种不足,

输入字符没有异常处理,字符过长也没有异常处理,

答案区空出也没有相应的提示等等问题的存在,

我会好好的面对这些问题,好好的解决这些问题。

作业5.2

标签:

原文地址:http://www.cnblogs.com/wuxutao/p/4488605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!