• 作业5 四则运算 测试与封装 5.1


    我这次的结对伙伴是:罗林杰 博客:http://www.cnblogs.com/yihou2ni/

    在这次结对任务中,我跟我的结对伙伴各自负责写白盒、黑盒的测试。类的封装是一起合作完成的。

    1.第一阶段目标 - 把计算的功能封装成类。

    mySize.java

    package packageClass;
    import java.util.*;
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    public class mySize extends JFrame implements ActionListener{
        JLabel L1,L2,L3,L4,Feedback,Questions; 
        JTextField inputtext,answertxet; 
        JButton SkinButton,NextBotton,EnterBotton,FactBotton;
        int number,i,n,op,count=0,backcolor=0;
        int d1,d2, d3;
        double result;
        boolean Factorial=false;Core core = new Core();
        public mySize()
        {
            super("四则运算");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400,250);
            this.setLocationRelativeTo(null);
            L1=new JLabel("输入出题数:");
            L2=new JLabel("题目:");
            L3=new JLabel("输入答案:");
            L4=new JLabel("反馈信息:");
            Feedback=new JLabel(" ");
            SkinButton=new JButton("换肤");
            NextBotton=new JButton("下一题");
            EnterBotton=new JButton("提交答案");FactBotton=new JButton("阶乘运算");
            Questions=new JLabel(" ");
            inputtext=new JTextField(10);
            answertxet=new JTextField(10);
            inputtext.addActionListener(this);
            answertxet.addActionListener(this);
            SkinButton.addActionListener(this);
            NextBotton.addActionListener(this);EnterBotton.addActionListener(this);FactBotton.addActionListener(this);
            GridLayout layout=new GridLayout(7,2);
            getContentPane().setLayout(layout);
            getContentPane().setBackground(Color.white);
            L1.setHorizontalAlignment(L1.RIGHT);
            L2.setHorizontalAlignment(L2.RIGHT);
            L3.setHorizontalAlignment(L3.RIGHT);
            L4.setHorizontalAlignment(L4.RIGHT);
            Feedback.setHorizontalAlignment(Feedback.CENTER);
            Questions.setHorizontalAlignment(Feedback.CENTER);
            inputtext.setHorizontalAlignment(inputtext.CENTER);
            add(L1);add(inputtext);
            add(L2);add(Questions);
            add(L3);add(answertxet);
            add(L4);add(Feedback);
            add(EnterBotton);add(NextBotton);add(SkinButton);add(FactBotton);
            setVisible(true);
            setResizable(false);
        }
    
        public static void main(String[] args)
    
        {
            mySize frame=new mySize();
        }
        
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==inputtext)
            {
                
                String str=inputtext.getText();Feedback.setText("");
                boolean isAllDigit = true;//定义一个boolean值,用来表示是否包含其他字符
                for(int i=0 ;i<str.length(); i++)//循环遍历字符串   
                    if(!Character.isDigit(str.charAt(i))){isAllDigit=false;}//用char包装类中的判断数字的方法判断每一个字符
                if(isAllDigit)
                {
                    number=Integer.parseInt(str);
                    if(number<6&&number>0)
                    {    
                    result=core.creatQuestion(Questions,d1,d2,d3,op);
                    }
                    else
                        Feedback.setText("出题数限制在5以内");
                }
                else
                    Feedback.setText("错误!!输入数据含有其他字符!");
            }        
            
            else if(e.getSource()==EnterBotton||e.getSource()==answertxet){
                String str=answertxet.getText();Feedback.setText("");
                
                    double a=Double.parseDouble(str);
                    if(Factorial==false){
                        if(count<core.CorrectNum(a,result))
                        {
                        count=core.CorrectNum(a,result) ;
                        Feedback.setText("答案正确!请继续做题。");
                        }
                        else
                            Feedback.setText("答错了!正确答案:"+String.format("%.2f",result)); 
                   }
                    else
                    {
                    if(a==core.Factorial(d1)){Feedback.setText("答案正确!");}
                    else
                    Feedback.setText("答错了!正确答案为"+core.Factorial(d1));
                    Factorial=false;
                    }
                
                }
            
            else if(e.getSource()==SkinButton){
                switch(backcolor)
                {
                case 0:this.getContentPane().setBackground(Color.red);break;
                case 1:this.getContentPane().setBackground(Color.black);break;
                case 2:this.getContentPane().setBackground(Color.white);break;
                case 3:this.getContentPane().setBackground(Color.yellow);break;
                case 4:this.getContentPane().setBackground(Color.blue);break;
                case 5:this.getContentPane().setBackground(Color.GREEN);break;
                }
                backcolor++;
                backcolor=backcolor%6;
            }
            else if(e.getSource()==FactBotton){
                d1=(int) (Math.random()*10);
                L2.setText("题目:");Questions.setText(d1+"!");    Factorial=true;inputtext.setText("");answertxet.setText("");Feedback.setText("");
            }
            else if(e.getSource()==NextBotton){
                
                if(number>1)
                {
                    number--;
                        result=core.creatQuestion(Questions,d1,d2,d3,op);
                        answertxet.setText("");Feedback.setText("");            
                }
                else
                    Feedback.setText("题目已答完,答对"+count+"");
            }
        }
    }
    View Code

    core.java

    package packageClass;
    
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class Core {
        static double result=0;
        static int count=0;
        
        public int Factorial(int a)
        {
            int i,sum=1;
            for(i=1;i<=a;i++)
            {
                sum=sum*i;
            }
            return sum;
        }
        
        public double creatQuestion(JLabel Questions,int d1,int d2,int d3,int op) {
                op=(int) (Math.random()*5);
                d1=(int) (Math.random()*200-100);
                d2=(int) (Math.random()*200-100);
                d3=(int) (Math.random()*200-100);
                switch(op)
                {
                case 0:result=(d1+d2*d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")×("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+""+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"×"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"×("+String.valueOf(d3)+")=");break;
                case 1:result=(d1+(double)d2/d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+""+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                case 2:result=(d1-d2*d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")×("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+""+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"×"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"×("+String.valueOf(d3)+")=");break;
                case 3:result=(d1-(double)d2/d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+""+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                case 4:result=((d1*d2)/(double)d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"×("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"×("+String.valueOf(d2)+""+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"×"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"×"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                } return result;
            }
        
        public int CorrectNum(double a,double result) {
                                if(Math.abs(a-result)<0.01){
                                    count++;
                                    }
                                else
                                    {
                                    }
                             return count;
        }
        
        
        }

    2.设计测试用例:用白盒与黑盒测试设计技术,为计算核心设计测试用例。

     白盒测试:

      一、逻辑覆盖法

    黑盒测试:

    1.验证阶乘,四则运算等功能的正确性及容错功能

    3.在实验环境中(如MyEclipse集成开发环境+Junit测试框架)运行测试用例,分析测试结果,找出程序问题,给出改进前后的代码对照。

    测试用例:

    package packageClass;
    
    import static org.junit.Assert.*;
    
    import javax.swing.JLabel;
    
    import org.junit.Before;
    import org.junit.Test;
    
    public class CoreTest {
        private static Core calculate = new Core();
        @Before
        public void setUp() throws Exception {
            
        }
    
        @Test
        public void testFactorial() {
             assertEquals(2, calculate.Factorial(2));//验证阶乘的正确性
        }
    
        @Test
        public void testCreatQuestion() {
            
    
            assertEquals(21, (int)calculate.creatQuestion(3, 4, 5, 0));
    
        }
    
        @Test
        public void testCorrectNum() {
            assertEquals(1,calculate.CorrectNum(7, 7));
        }
    
    }

    程序修改前:

    import java.util.*;
    import java.math.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class SiZi extends JFrame implements ActionListener{
        JLabel L1,L2,L3,L4,L5,Ltimu;
        JTextField inputtext,answertxet;
        JButton B1,B2,B3,B4;
        int number,i,n,op,tid=1,count=0,backcolor=0,jieflag=0;
        int d1,d2, d3;
        public SiZi()
        {
            super("四则运算");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400,250);
            this.setLocationRelativeTo(null);
            L1=new JLabel("输入出题数:");
            L2=new JLabel("题目:");
            L3=new JLabel("输入答案:");
            L4=new JLabel("反馈信息:");
            L5=new JLabel(" ");
            B1=new JButton("换肤");
            B2=new JButton("下一题");
            B3=new JButton("提交答案");B4=new JButton("阶乘运算");
            Ltimu=new JLabel(" ");
            inputtext=new JTextField(10);
            answertxet=new JTextField(10);
            inputtext.addActionListener(this);
            answertxet.addActionListener(this);
            B1.addActionListener(this);
            B2.addActionListener(this);B3.addActionListener(this);B4.addActionListener(this);
            GridLayout layout=new GridLayout(7,2);
            getContentPane().setLayout(layout);
            getContentPane().setBackground(Color.white);
            L1.setHorizontalAlignment(L1.RIGHT);
            L2.setHorizontalAlignment(L2.RIGHT);
            L3.setHorizontalAlignment(L3.RIGHT);
            L4.setHorizontalAlignment(L4.RIGHT);
            L5.setHorizontalAlignment(L5.CENTER);
            Ltimu.setHorizontalAlignment(L5.CENTER);
            inputtext.setHorizontalAlignment(inputtext.CENTER);
            add(L1);add(inputtext);
            add(L2);add(Ltimu);
            add(L3);add(answertxet);
            add(L4);add(L5);
            add(B3);add(B2);add(B1);add(B4);
            setVisible(true);
            setResizable(false);
        }
        
        public static void main(String[] args)
    
        {
            SiZi frame=new SiZi();
        }
        
    
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==inputtext)
            {
                String str=inputtext.getText();L5.setText("");
                boolean isAllLetter = true;//定义一个boolean值,用来表示是否不含包含数字
                for(int i=0 ; i<str.length() ; i++)//循环遍历字符串   
                    if(!Character.isDigit(str.charAt(i))){   //用char包装类中的判断数字的方法判断每一个字符
                        isAllLetter = false;
                    }
                if(isAllLetter){
                    number=Integer.parseInt(str);n=number;
                    if(number>0&&number<6)
                    {
                        op=(int) (Math.random()*5);
                        d1=(int) (Math.random()*200-100);
                        d2=(int) (Math.random()*200-100);
                        d3=(int) (Math.random()*200-100);
                        switch(op)
                        {
                        case 0:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")×("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")×"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"×"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"×("+String.valueOf(d3)+")=");break;
                        case 1:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                        case 2:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")×("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")×"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"×"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"×("+String.valueOf(d3)+")=");break;
                        case 3:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                        case 4:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"×("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"×("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"×"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"×"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                            }    
                        tid=1;count=0;
                        L2.setText("题目"+tid+": ");
                        }
                    else
                    L5.setText("出题数限制在5以内");
                    }
                else
                    L5.setText("错误!!输入数据含有其他字符!");
                }
            
            else if(e.getSource()==B3||e.getSource()==answertxet){
                String str=answertxet.getText();L5.setText("");
                boolean isAllLetter = true;//定义一个boolean值,用来表示是否不含包含数字
                for(int i=0 ; i<str.length() ; i++)//循环遍历字符串   
                    if(!Character.isDigit(str.charAt(i))){   //用char包装类中的判断数字的方法判断每一个字符
                        isAllLetter = false;
                    }
                if(isAllLetter){
                double a=Double.parseDouble(str);
                if(jieflag==0){
                    switch(op)
                    {
                    case 0:
                        if(d1+(d2*d3)==a){
                            L5.setText("答案正确!请继续做题。");
                            count++;
                            }
                        else{
                            L5.setText("答错了!正确答案:"+(d1+d2*d3));
                            };break;
                            case 1:
                                if(Math.abs(a-((d1+d2)/(double)d3))<0.01){
                                    L5.setText("答案正确!请继续做题。");
                                    count++;
                                    }
                                else{
                                    L5.setText("答错了!正确答案为:"+String.format("%.2f", (d1+(double)d2/d3))); 
                                    };break;
                                    case 2:
                                        if((d1-(d2*d3))==a){
                                            L5.setText("答案正确!请继续做题。");
                                            count++;
                                            }
                                        else{
                                            L5.setText("答错了!正确答案为:"+(d1-d2*d3));
                                            };break;
                                            case 3:
                                                if(Math.abs(a-((d1-d2)/(double)d3))<0.01){
                                                    L5.setText("答案正确!请继续做题。");
                                                    count++;
                                                    }
                                                else{
                                                    L5.setText("答错了!正确答案为:"+String.format("%.2f", (d1-(double)d2/d3)));
                                                    };break;
                                                    case 4:
                                                        if(Math.abs(a-((d1*d2)/(double)d3))<0.01){
                                                            L5.setText("答案正确!请继续做题。");
                                                            count++;
                                                            }
                                                        else{
                                                            L5.setText("答错了!正确答案为:"+String.format("%.2f", (d1*(double)d2/d3)));
                                                            };break;
                                                            }
                    }
                else
                {
                    int i,sum=1;
                    for(i=1;i<=d1;i++){
                        sum=sum*i;
                    }
                    if(a==sum){L5.setText("答案正确!");}
                    else
                    L5.setText("答错了!正确答案为"+sum);
                    jieflag=0;
                }
            }
            else
                L5.setText("错误!!输入数据含有其他字符!");}
            
            
            else if(e.getSource()==B1){
                switch(backcolor)
                {
                case 0:this.getContentPane().setBackground(Color.red);break;
                case 1:this.getContentPane().setBackground(Color.black);break;
                case 2:this.getContentPane().setBackground(Color.white);break;
                case 3:this.getContentPane().setBackground(Color.yellow);break;
                case 4:this.getContentPane().setBackground(Color.blue);break;
                case 5:this.getContentPane().setBackground(Color.GREEN);break;
                }
                backcolor++;
                backcolor=backcolor%6;
            }
            else if(e.getSource()==B4){
                d1=(int) (Math.random()*10);
                L2.setText("题目:");Ltimu.setText(d1+"!");    jieflag=1;inputtext.setText("");answertxet.setText("");L5.setText("");
            }
            else if(e.getSource()==B2){
                
                if(n>1)
                {
                    n--;
                        op=(int) (Math.random()*5);
                        d1=(int) (Math.random()*200)-100;
                        d2=(int) (Math.random()*200)-100;
                        d3=(int) (Math.random()*200)-100;
                        switch(op)
                         {
                        case 0:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")×("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")×"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"×"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"×("+String.valueOf(d3)+")=");break;
                        case 1:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                        case 2:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")×("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")×"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"×"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"×("+String.valueOf(d3)+")=");break;
                        case 3:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                        case 4:
                            if(d2<0&&d3<0)Ltimu.setText(String.valueOf(d1)+"×("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"×("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                            else if (d2>0&&d3>0)Ltimu.setText(String.valueOf(d1)+"×"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                            else if (d2<0&&d3>0)Ltimu.setText(String.valueOf(d1)+"×"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                         }
                        tid++;
                        L2.setText("题目"+tid+": ");
                        answertxet.setText("");L5.setText("");            
                }
                else
                    L5.setText("题目已答完,答对"+count+"题,答错"+(number-count)+"题");
            }
        }
    }
    View Code

    程序修改后: 

    package packageClass;
    import java.util.*;
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    public class mySize extends JFrame implements ActionListener{
        JLabel L1,L2,L3,L4,Feedback,Questions; 
        JTextField inputtext,answertxet; 
        JButton SkinButton,NextBotton,EnterBotton,FactBotton;
        int number,i,n,op,tid=1,count=0,backcolor=0;
        int d1,d2, d3;
        double result;
        boolean Factorial=false;Core core = new Core();
        public mySize()
        {
            super("四则运算");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400,250);
            this.setLocationRelativeTo(null);
            L1=new JLabel("输入出题数:");
            L2=new JLabel("题目:");
            L3=new JLabel("输入答案:");
            L4=new JLabel("反馈信息:");
            Feedback=new JLabel(" ");
            SkinButton=new JButton("换肤");
            NextBotton=new JButton("下一题");
            EnterBotton=new JButton("提交答案");FactBotton=new JButton("阶乘运算");
            Questions=new JLabel(" ");
            inputtext=new JTextField(10);
            answertxet=new JTextField(10);
            inputtext.addActionListener(this);
            answertxet.addActionListener(this);
            SkinButton.addActionListener(this);
            NextBotton.addActionListener(this);EnterBotton.addActionListener(this);FactBotton.addActionListener(this);
            GridLayout layout=new GridLayout(7,2);
            getContentPane().setLayout(layout);
            getContentPane().setBackground(Color.white);
            L1.setHorizontalAlignment(L1.RIGHT);
            L2.setHorizontalAlignment(L2.RIGHT);
            L3.setHorizontalAlignment(L3.RIGHT);
            L4.setHorizontalAlignment(L4.RIGHT);
            Feedback.setHorizontalAlignment(Feedback.CENTER);
            Questions.setHorizontalAlignment(Feedback.CENTER);
            inputtext.setHorizontalAlignment(inputtext.CENTER);
            add(L1);add(inputtext);
            add(L2);add(Questions);
            add(L3);add(answertxet);
            add(L4);add(Feedback);
            add(EnterBotton);add(NextBotton);add(SkinButton);add(FactBotton);
            setVisible(true);
            setResizable(false);
        }
    
        public static void main(String[] args)
    
        {
            mySize frame=new mySize();
        }
        
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==inputtext)
            {
                
                String str=inputtext.getText();Feedback.setText("");
                boolean isAllDigit = true;//定义一个boolean值,用来表示是否包含其他字符
                for(int i=0 ;i<str.length(); i++)//循环遍历字符串   
                    if(!Character.isDigit(str.charAt(i))){isAllDigit=false;}//用char包装类中的判断数字的方法判断每一个字符
                if(isAllDigit)
                {
                    number=Integer.parseInt(str);
                    if(number<6&&number>0)
                    {    
                    result=core.creatQuestion(Questions,d1,d2,d3,op);
                    }
                    else
                        Feedback.setText("出题数限制在5以内");
                }
                else
                    Feedback.setText("错误!!输入数据含有其他字符!");
            }        
            
            else if(e.getSource()==EnterBotton||e.getSource()==answertxet){
                String str=answertxet.getText();Feedback.setText("");
                
                    double a=Double.parseDouble(str);
                    if(Factorial==false){
                        count=core.CorrectNum(a,result,Feedback) ;
                   }
                    else
                    {
                    if(a==core.Factorial(d1)){Feedback.setText("答案正确!");}
                    else
                    Feedback.setText("答错了!正确答案为"+core.Factorial(d1));
                    Factorial=false;
                    }
                
                }
            
            else if(e.getSource()==SkinButton){
                switch(backcolor)
                {
                case 0:this.getContentPane().setBackground(Color.red);break;
                case 1:this.getContentPane().setBackground(Color.black);break;
                case 2:this.getContentPane().setBackground(Color.white);break;
                case 3:this.getContentPane().setBackground(Color.yellow);break;
                case 4:this.getContentPane().setBackground(Color.blue);break;
                case 5:this.getContentPane().setBackground(Color.GREEN);break;
                }
                backcolor++;
                backcolor=backcolor%6;
            }
            else if(e.getSource()==FactBotton){
                d1=(int) (Math.random()*10);
                L2.setText("题目:");Questions.setText(d1+"!");    Factorial=true;inputtext.setText("");answertxet.setText("");Feedback.setText("");
            }
            else if(e.getSource()==NextBotton){
                
                if(number>1)
                {
                    number--;
                        result=core.creatQuestion(Questions,d1,d2,d3,op);
                        answertxet.setText("");Feedback.setText("");            
                }
                else
                    Feedback.setText("题目已答完,答对"+count+"题");
            }
        }
    }
    View Code
    package packageClass;
    
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class Core {
        static double result=0;
        static int count=0;
        
        public int Factorial(int a)
        {
            int i,sum=1;
            for(i=1;i<=a;i++)
            {
                sum=sum*i;
            }
            return sum;
        }
        
        public double creatQuestion(JLabel Questions,int d1,int d2,int d3,int op) {
                op=(int) (Math.random()*5);
                d1=(int) (Math.random()*200-100);
                d2=(int) (Math.random()*200-100);
                d3=(int) (Math.random()*200-100);
                switch(op)
                {
                case 0:result=(d1+d2*d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")×("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")×"+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"×"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"×("+String.valueOf(d3)+")=");break;
                case 1:result=(d1+(double)d2/d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"+("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"+"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                case 2:result=(d1-d2*d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")×("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")×"+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"×"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"×("+String.valueOf(d3)+")=");break;
                case 3:result=(d1-(double)d2/d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"-("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"-"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                case 4:result=((d1*d2)/(double)d3);
                    if(d2<0&&d3<0)Questions.setText(String.valueOf(d1)+"×("+String.valueOf(d2)+")÷("+String.valueOf(d3)+")=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"×("+String.valueOf(d2)+")÷"+String.valueOf(d3)+"=");
                    else if (d2>0&&d3>0)Questions.setText(String.valueOf(d1)+"×"+String.valueOf(d2)+"÷"+String.valueOf(d3)+"=");
                    else if (d2<0&&d3>0)Questions.setText(String.valueOf(d1)+"×"+String.valueOf(d2)+"÷("+String.valueOf(d3)+")=");break;
                } return result;
            }
        
        public int CorrectNum(double a,double result,JLabel Feedback) {
            
                                if(Math.abs(a-result)<0.01){
                                    Feedback.setText("答案正确!请继续做题。");
                                    count++;
                                    }
                                else
                                    Feedback.setText("答错了!正确答案:"+String.format("%.2f",result)); 
                             return count;
        }
        
        
        }
    View Code
  • 相关阅读:
    在线图片压缩
    wiki-editor语法
    Android 4.0.4模拟器安装完全教程(图文)
    Javascript中的void
    守护进程
    jQuery编程的最佳实践
    JavaScript内存优化
    vim编程技巧
    MySQL表的四种分区类型
    SQL中的where条件,在数据库中提取与应用浅析
  • 原文地址:https://www.cnblogs.com/junkaiwang/p/4469205.html
Copyright © 2020-2023  润新知