软件工程个人作业(2)

 

源代码:package zuoye2;

 

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.*;

 

class jiemian

{

    public jiemian()

    {

    JFrame  jf=new JFrame("四则运算");

    jf.setBounds(200, 100, 500, 500);

    jf.setLayout(null);

    Container c=jf.getContentPane();

    JCheckBox j1=new JCheckBox("有乘除法");

    JCheckBox j2=new JCheckBox("有括号");

    JCheckBox j3=new JCheckBox("有负数");

    JCheckBox j4=new JCheckBox("有余数");

    JLabel l1=new JLabel("请输入数值范围");

    JLabel l4=new JLabel("请输入参数个数(1—10)");

    JLabel l2=new JLabel("—");

    JLabel l3=new JLabel("请输入题目数目(1-30)");

    JTextField t1=new JTextField("0");

    JTextField t2=new JTextField();

    JTextField t3=new JTextField();

    JTextField t4=new JTextField();

    j1.setBounds(100, 100, 80,20);

    j2.setBounds(100, 130, 80,20);

    l4.setBounds(100, 160, 180,20);

    j3.setBounds(100, 220, 80,20);

    j4.setBounds(100, 250, 80,20);

    t1.setBounds(100, 190, 50,20);

    l1.setBounds(100, 280, 150,20);

    t2.setBounds(100, 310, 50,20);

    t3.setBounds(180, 310, 50,20);

    l2.setBounds(160, 310, 30,20);

    l3.setBounds(100, 340, 150,20);

    t4.setBounds(100, 370, 50,20);

    c.add(j1);

    c.add(j2);

    c.add(j3);

    c.add(l4);

    c.add(j4);

    c.add(t1);

    c.add(l1);

    c.add(t2);

    c.add(t3);

    c.add(l2);

    c.add(l3);

    c.add(t4);

    JButton b1=new JButton("确定");

    JButton b2=new JButton("退出");

    b1.setBounds(350, 150,70, 50);

    b2.setBounds(350, 300,70, 50);

    c.add(b1);

    c.add(b2);

    b1.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e)

        {

            jf.dispose();

            String a1,a2,a3,a4,a5;

            int c1,c2,c3,c4,c5;

            boolean f1=false,f2=false,f3=false,f4=false,f5=false;

            a1=t1.getText().toString();

            a2=t2.getText().toString();

            a3=t3.getText().toString();

            a4=t4.getText().toString();

            if(a2.equals("")||a3.equals(""))

            {

                JOptionPane.showMessageDialog(null, "范围不能为空", "范围不能为空", JOptionPane.ERROR_MESSAGE);

                System.exit(0);

            }

            else if(a4.equals(""))

            {

                JOptionPane.showMessageDialog(null, "数目不能为空", "数目不能为空", JOptionPane.ERROR_MESSAGE);

                System.exit(0);

            }

            else if(j2.isSelected())

            {

                if (a1.equals(""))

                {

                JOptionPane.showMessageDialog(null, "参数个数不能为空", "参数个数不能为空", JOptionPane.ERROR_MESSAGE);

                System.exit(0);

                }

                else if(Integer.parseInt(a1)>10||Integer.parseInt(a1)<=0)

                    {

                    JOptionPane.showMessageDialog(null, "参数个数超出", "参数个数超出", JOptionPane.ERROR_MESSAGE);

                    System.exit(0);}

            }

            else if(Integer.parseInt(a4)<=0||Integer.parseInt(a4)>30)

            {

                JOptionPane.showMessageDialog(null, "题目数目不正确", "题目数目不正确", JOptionPane.ERROR_MESSAGE);

                System.exit(0);

            }

            else if(Integer.parseInt(a3)<=Integer.parseInt(a2))

            {

                JOptionPane.showMessageDialog(null, "范围不正确", "范围不正确", JOptionPane.ERROR_MESSAGE);

                System.exit(0);

            }

            if(j1.isSelected())

            {

                f1=true;

            }

            if(j2.isSelected())

            {

                f2=true;

            }

            if(j3.isSelected())

            {

                f3=true;

            }

            if(j4.isSelected())

            {

                f4=true;

            }

            c1=Integer.parseInt(a1);

            c2=Integer.parseInt(a2);

            c3=Integer.parseInt(a3);

            c4=Integer.parseInt(a4);

            yunsuan a=new yunsuan(c1,c2,c3,c4,f1,f2,f3,f4);

        }

 

    });

    b2.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e)

        {

            jf.dispose();

        }

 

 

 

 

    });

    jf.setVisible(true);

    jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

}

package zuoye2;

 

 

public class m extends jiemian

{

   

    public static void main(String[] args)

    {

        // TODO Auto-generated method stub

        jiemian a=new jiemian();

    }

 

}

 

 

 

}

            else

            {

                if(e[i]==1)

                {

                    a[i]=r1[i].nextInt(c3-c2)+c2;

                    b[i]=r2[i].nextInt(c3-c2)+c2;

                    d[i]=a[i]+b[i];

                    a1[i]=String.valueOf(a[i]);

                    b1[i]=String.valueOf(b[i]);

                    d1[i]=String.valueOf(d[i]);

                    j1[i]=new JLabel(a1[i]);

                    j2[i]=new JLabel("+");

                    j3[i]=new JLabel(b1[i]);

                    j4[i]=new JLabel("=");

                   

                }

                if(e[i]==2)

                {

                    int x;

                    a[i]=r1[i].nextInt(c3-c2)+c2;

                    b[i]=r2[i].nextInt(c3-c2)+c2;

                    if(b[i]>a[i])

                    {

                        x=b[i];

                        b[i]=a[i];

                        a[i]=x;

                       

                    }

                    d[i]=a[i]-b[i];

                    a1[i]=String.valueOf(a[i]);

                    b1[i]=String.valueOf(b[i]);

                    d1[i]=String.valueOf(d[i]);

                    j1[i]=new JLabel(a1[i]);

                    j2[i]=new JLabel("-");

                    j3[i]=new JLabel(b1[i]);

                    j4[i]=new JLabel("=");

                   

                }

            }

           

            if(e[i]==3)

            {

                a[i]=r1[i].nextInt(c3-c2)+c2;

                b[i]=r2[i].nextInt(c3-c2)+c2;

                d[i]=a[i]*b[i];

                a1[i]=String.valueOf(a[i]);

                b1[i]=String.valueOf(b[i]);

                d1[i]=String.valueOf(d[i]);

                j1[i]=new JLabel(a1[i]);

                j2[i]=new JLabel("X");

                j3[i]=new JLabel(b1[i]);

                j4[i]=new JLabel("=");

               

            }

            if(f4==false)

            {

            if(e[i]==4)

            {

                d[i]=r1[i].nextInt(c3-c2)+c2;

                b[i]=r2[i].nextInt(c3-c2)+c2;

                a[i]=b[i]*d[i];

                while(d[i]==0||b[i]==0)

                {

                    r1[i]=new Random();

                    r2[i]=new Random();

                    d[i]=r1[i].nextInt(20);

                    b[i]=r2[i].nextInt(20);

                    a[i]=b[i]*d[i];

                }

               

                a1[i]=String.valueOf(a[i]);

                b1[i]=String.valueOf(b[i]);

                d1[i]=String.valueOf(d[i]);

                j1[i]=new JLabel(a1[i]);

                j2[i]=new JLabel("/");

                j3[i]=new JLabel(b1[i]);

                j4[i]=new JLabel("=");

               

               

            }

            }

            else

            {

                if(e[i]==4)

                {

                    j7[i]=new JLabel("余");

                    a[i]=r1[i].nextInt(c3-c2)+c2;

                    b[i]=r2[i].nextInt(c3-c2)+c2;

                    while(a[i]==0||b[i]==0)

                    {

                        r1[i]=new Random();

                        r2[i]=new Random();

                        d[i]=r1[i].nextInt(20);

                        b[i]=r2[i].nextInt(20);

                        a[i]=b[i]*d[i];

                    }

                   

                    a1[i]=String.valueOf(a[i]);

                    b1[i]=String.valueOf(b[i]);

                    d1[i]=String.valueOf(d[i]);

                    j1[i]=new JLabel(a1[i]);

                    j2[i]=new JLabel("/");

                    j3[i]=new JLabel(b1[i]);

                    j4[i]=new JLabel("=");

                   

                   

 

                }

            }

            if(e[i]==4&&f4==true)

            {

            j1[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

            j++;

            j2[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

            j++;

            j3[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

            j++;

            j4[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

            j++;

            j5[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

            j++;

            j7[i].setBounds(65+j*42,50+(k/2)*32, 40, 30);

            j++;

            j6[i].setBounds(50+j*42,50+(k/2)*32, 40, 30);

            j++;

            j++;

            j++;

            k++;

            if(k!=0&&k%2==0)

            {

                j=0;

            }

            c.add(j1[i]);

            c.add(j2[i]);

            c.add(j3[i]);

            c.add(j4[i]);

            c.add(j5[i]);

            c.add(j6[i]);

            c.add(j7[i]);

            }

            else

            {

                j1[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

                j++;

                j2[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

                j++;

                j3[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

                j++;

                j4[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

                j++;

                j5[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);

                j++;

                j++;

                j++;

                j++;

                j++;

                k++;

                if(k!=0&&k%2==0)

                {

                    j=0;

                }

                c.add(j1[i]);

                c.add(j2[i]);

                c.add(j3[i]);

                c.add(j4[i]);

                c.add(j5[i]);

            }

        }

        jf.setVisible(true);

        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

 

}

 

设计思路:

1.建立一个frame面板,通过添加复选框来选择是否有乘除法,是否有余数,是否有括号以及是否有负数

2.通过添加文本框来控制计算范围和题目数目。

3.当点击确定按钮,当前窗口关闭,数据提交,并新建一个frame窗口,frame窗口大小根据题目数目而定。

4.通过random产生随机数来控制计算题目以及加减乘除(0-3分别代表加减乘除,若没有选择有乘除,则产生0或1,从而将乘除运算排除掉)

5.根据是否选择有余数来控制除法。若没有余数,则将生成的两个随机数之积作为被除数,第二个随机数作为除数。

6.若选择有负数,加减不做处理,否则比较减数与被减数大小。若减数大于被减数,则将二者交换。

7.通过建立JLabel数组来向窗口输出题目,题目坐标按照题目数累加

8.通过建立文本框来填入答案

9.括号和检验重复没做。

posted @ 2016-05-25 21:25  dkjjjj  阅读(179)  评论(0编辑  收藏  举报