编译环境:Ecllipse

开发人员:朱浩龙 学号:201306114324

              叶煜稳 学号:201306114323 博客:http://www.cnblogs.com/doubi2wy/

最后开发时间:2015—04—09

实现功能:

  1. 可以出表达式里含有负整数(负整数最小不小于-100)的题目,且负数需要带括号,用户输入的结果不用带括号。如: 2*(-4) = -8
  2. 用户答题结束以后,程序可以显示用户答题所用的时间
  3. 用户可以选择出题的个数(最多不能超过5个题目),答题结束可以显示用户答错的题目个数和答对的题目个数
  4. 程序可以出单个整数阶乘的题目:如:4!=24

分工:

      程序的主体和答案的算法是我和队友一起完成的,表达式中含有负数以及用户可以选择出题数是我独立完成的;先是答题时间以及阶层是叶煜稳完成的。

总结:

      这次的程序编写有很多的困难,一开始的不适应以及双方写代码方式的不同都造成了很多的困难。程序本身还存在着许多的问题,例如答案的算法有时候不正确,程序有时候会BUG,这些都是还没有解决的问题。四则运算本身不是很难,但是如果涉及多个运算符的话算法的思考就会变得复杂,解决算法的问题是编程中花费时间最多的,但是通过算法的分析设计使得我对于java算法的立即更加的深刻。在这次的实验中,界面非常的简陋,这是由于时间问题,我希望下次的界面能够做的更好。通过这次实验,让我了解了结对编程的好处。首先在编程的过程中可以避免很多细节上的问题,例如语句的缺少,算法的错误等都可以及时的发现。但是在结对编程的过程中也会出现类是吵架的现象出现,这是两个人的想法以及个人习惯的碰撞。虽然结对编程使用的时间好像比预计的时间要多。但是我相信多次结对变成后会变得更快。

package ZhuTi;

import java.awt.Color;

public class 四则运算 {

    private JFrame frame;
    private JTextField T1;
    static long startTime;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    四则运算 window = new 四则运算();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        });
    }
    int i,j,count=0;
    int c1,c2;
    double []a=new double [10];
    int []c=new int [10];
    int []s1=new int [10];
    char f = 0;
    String s2="";
    String s=null;
    long ST1;
    private JTextField T2;

    /**
     * Create the application.
     */
public 四则运算() {
    startTime = System.currentTimeMillis(); 
    
    
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        
        final JButton B1 = new JButton("\u5F00\u59CB\u7B54\u9898");        
        final JLabel L1 = new JLabel("");
                
        T1 = new JTextField();
        T1.setColumns(10);
        T1.setVisible(false);
        
        final JLabel L2 = new JLabel("");
        
        final JLabel L3 = new JLabel("");
        
        JLabel l3 = new JLabel("\u6BCF\u6B21\u7B54\u9898\u7684\u6570\u76EE\uFF08\u5C0F\u4E8E5\uFF09");
        
        T2 = new JTextField();
        T2.setColumns(10);
        T2.setText("5");
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(160)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(L2)
                        .addComponent(L3))
                    .addContainerGap(196, Short.MAX_VALUE))
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(151)
                    .addComponent(B1, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(185, Short.MAX_VALUE))
                .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                    .addContainerGap(97, Short.MAX_VALUE)
                    .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addComponent(l3)
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addComponent(T2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(14))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addComponent(L1)
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addComponent(T1, GroupLayout.PREFERRED_SIZE, 151, GroupLayout.PREFERRED_SIZE)))
                    .addGap(115))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(36)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(l3)
                        .addComponent(T2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addComponent(B1, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(T1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(L1))
                    .addGap(18)
                    .addComponent(L2)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(L3)
                    .addContainerGap(78, Short.MAX_VALUE))
        );
        frame.getContentPane().setLayout(groupLayout);
        T2.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                
                c2=(int)(Double.parseDouble(T2.getText()));
                if(c2>5) 
                    {
                    T2.setText("5");
                    c2=5;
                    }
            }
        });
        B1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                L1.setVisible(true);
                L2.setVisible(true);
                L3.setVisible(true);
                L1.setText(" ");
                L2.setText(" ");
                L3.setText(" ");
                T1.setVisible(true);
                T2.setEditable(false);
                B1.setVisible(false);
                c2=(int)(Double.parseDouble(T2.getText()));
                if(c2>5) 
                    {
                    T2.setText("5");
                    c2=5;
                    }
                ST1 = System.currentTimeMillis(); 
                c1=0;
                String s2=methodA();
                System.out.println(a[0]);
                L1.setText(""+s2+"=");
                }
        });
        T1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                
                int d=(int)(Double.parseDouble(T1.getText()));
                if(a[0]==d)
                {
                    L2.setText("正确!");
                    L3.setText(" ");
                    for(i=0;i<999;i++);
                    T1.setText("");
                    if(c1<c2)
                    {
                    String s2=methodA();
                    System.out.println(a[0]);
                    L1.setText(""+s2+"=");
                    c1++;
                    count++;
                    }
                    if(c1==c2)
                    {
                        long endTime1 = System.currentTimeMillis(); 
                        JOptionPane.showMessageDialog(null, "答题耗时耗时" 
                                + (endTime1 - ST1) / 1000 + "秒"+"\n答对"+(count)+"题"+"\n答错"+(c2-count)+"题", "消息提示", 
                                JOptionPane.ERROR_MESSAGE);
                        B1.setVisible(true);
                        T2.setEditable(true);
                        L1.setVisible(false);
                        L2.setVisible(false);
                    }
                }
                else
                {
                    L2.setText("错误!");
                    L3.setText("正确答案是"+a[0]);
                    T1.setText("");
                    if(c1<c2)
                    {            
                    String s2=methodA();
                    System.out.println(a[0]);
                    L1.setText(""+s2+"=");
                    c1++;    
                    }
                    if(c1==c2)
                    {
                        long endTime1 = System.currentTimeMillis(); 
                        JOptionPane.showMessageDialog(null, "答题耗时耗时" 
                                + (endTime1 - ST1) / 1000 + "秒"+"\n答对"+(count)+"题"+"\n答错"+(c2-count)+"题", "消息提示", 
                                JOptionPane.ERROR_MESSAGE);
                        B1.setVisible(true);
                        T2.setEditable(true);
                        L1.setVisible(false);
                        L2.setVisible(false);
                        L3.setVisible(false);
                        T1.setVisible(false);
                    }
                }
                }
        });
        }
public String methodA(){
    String s2="";
    String s=null;
    int n=(int)(Math.random()*5);
    System.out.println(n);
    if(n==0)
        {
        a[0]=((int)(Math.random()*9+1));
        f='!';
        s2=s2+a[0]+f;
        System.out.println(a[0]);
        }
    else{
        for(i=0;i<=n;i++)
        {
            a[i]=((int)(Math.random()*200+1))-100;
        }
    for(i=0;i<n;i++)
    {
        c[i]=(int)(Math.random()*4+1);
    }
    for(i=0;i<=n;i++)
    {
        if(a[i]<0)
        {
            s="("+a[i]+")";
        }
        else s=""+a[i];
        s2=s2+s;
        switch(c[i])
        {
            case 1: f='+';break;
            case 2: f='-';break;
            case 3: f='*';break;
            case 4: f='/';break;
        }
        if(i<n) s2=s2+f;
    }}
    if(n==0) 
    {
        for(i=(int) a[0];i>1;i--)
        {
            System.out.println(i);
            System.out.println(a[0]);
            a[0]=a[0]*(i-1);
        }

        
    }
    else{
    for(i=0;i<=n;i++)
    {
                    if(c[i]==3)
                    {
                        a[i]=a[i]*a[i+1];
                        for(j=i;j<n;j++) 
                            {
                            a[j+1]=a[j+2];
                            c[j]=c[j+1];
                            }
                        i=0;
                        n--;
                    }
                    if(c[i]==4)
                    {
                        a[i]=a[i]/a[i+1];
                            for(j=i;j<n;j++) 
                            {
                            a[j+1]=a[j+2];
                            c[j]=c[j+1];
                            }
                    i=0;
                    n--;
                    }
                }
                for(i=0;i<=n;i++)
                {
                    if(c[i]==1)
                    {
                        a[i]=a[i]+a[i+1];
                        for(j=i;j<n;j++) 
                            {
                            a[j+1]=a[j+2];
                            c[j]=c[j+1];
                            }
                    i=0;
                    n--;
                    }
                    if(c[i]==2)
                    {
                        a[i]=a[i]-a[i+1];
                                                for(j=i;j<n;j++) 
                            {
                            a[j+1]=a[j+2];
                            c[j]=c[j+1];
                            }
                    i=0;
                    n--;
                    }
                }
    }
                return (s2);
}
}

 

程序界面:

posted on 2015-04-09 18:43  24朱浩龙  阅读(161)  评论(2编辑  收藏  举报