结对作业(1.0版)(bug1已修复)

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.*;
import java.util.Random;

public class SiZeYunSuan {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    int num=5,T=0,F=0,count=1;
    int ranswer=0;                 
    int uanswer=0;                 
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    private JTextField textField_7;
    private boolean stop = false;
    String str="";
    String z="";
    JTextPane textPane = new JTextPane();
    JButton button = new JButton("\u5F00\u59CB\u7B54\u9898");
    long startTime;
    long endTime;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SiZeYunSuan window = new SiZeYunSuan();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SiZeYunSuan() {
        initialize();    
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setResizable(false);
        
        JPanel panel = new JPanel();
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 448, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 487, Short.MAX_VALUE)
        );
        
        
        textPane.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 22));
        textPane.setEditable(false);
        textPane.setToolTipText("");
        textPane.setBackground(Color.WHITE);
        
        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(204, 204, 204));
        
        
        button.setBackground(Color.GREEN);
        
        JButton button_1 = new JButton("\u9000\u51FA");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        button_1.setBackground(Color.RED);
        
        JLabel lblNewLabel = new JLabel(" \u8F93\u5165\u9898\u6570\uFF1A");
        lblNewLabel.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
        lblNewLabel.setForeground(new Color(0, 0, 0));
        
        JLabel label = new JLabel(" \u7B54\u5BF9\u9898\u6570\uFF1A");
        label.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
        
        JLabel label_1 = new JLabel(" \u7B54\u9519\u9898\u6570\uFF1A");
        label_1.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
        
        JLabel label_2 = new JLabel(" \u4FE1\u606F\u53CD\u9988\uFF1A");
        label_2.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
        
        textField = new JTextField();
        textField.setColumns(10);
        
        textField_1 = new JTextField();
        textField_1.setForeground(Color.GREEN);
        textField_1.setEditable(false);
        textField_1.setColumns(10);
        
        textField_2 = new JTextField();
        textField_2.setForeground(Color.RED);
        textField_2.setEditable(false);
        textField_2.setColumns(10);
        
        textField_3 = new JTextField();
        textField_3.setFont(new Font("宋体", Font.PLAIN, 12));
        textField_3.setForeground(Color.RED);
        textField_3.setEditable(false);
        textField_3.setColumns(10);
        
        JLabel label_3 = new JLabel(" \u6B63\u786E\u7B54\u6848\uFF1A");
        label_3.setFont(new Font("Dialog", Font.PLAIN, 15));
        
        JLabel label_4 = new JLabel(" \u8F93\u5165\u7B54\u6848\uFF1A");
        label_4.setFont(new Font("Dialog", Font.PLAIN, 15));
        
        JLabel label_5 = new JLabel(" \u5F53\u524D\u9898\u6570\uFF1A");
        label_5.setFont(new Font("Dialog", Font.PLAIN, 15));
        
        textField_4 = new JTextField();
        textField_4.setEditable(false);
        textField_4.setColumns(10);
        
        textField_5 = new JTextField();
        textField_5.setColumns(10);
        
        textField_6 = new JTextField();
        textField_6.setFont(new Font("Yu Gothic", Font.BOLD, 14));
        textField_6.setForeground(Color.CYAN);
        textField_6.setEditable(false);
        textField_6.setColumns(10);
        
        JLabel label_6 = new JLabel(" \u6240\u7528\u65F6\u95F4\uFF1A");
        label_6.setFont(new Font("Dialog", Font.PLAIN, 15));
        
        textField_7 = new JTextField();
        textField_7.setFont(new Font("宋体", Font.PLAIN, 14));
        textField_7.setEditable(false);
        textField_7.setColumns(10);
        GroupLayout gl_panel_1 = new GroupLayout(panel_1);
        gl_panel_1.setHorizontalGroup(
            gl_panel_1.createParallelGroup(Alignment.TRAILING)
                .addGroup(gl_panel_1.createSequentialGroup()
                    .addGap(11)
                    .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                    .addGap(110))
                .addGroup(gl_panel_1.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                .addGroup(gl_panel_1.createSequentialGroup()
                                    .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                        .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                                    .addPreferredGap(ComponentPlacement.RELATED))
                                .addGroup(gl_panel_1.createSequentialGroup()
                                    .addComponent(label_3, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                                    .addPreferredGap(ComponentPlacement.RELATED)))
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addComponent(label_1)
                                .addPreferredGap(ComponentPlacement.RELATED)))
                        .addGroup(gl_panel_1.createSequentialGroup()
                            .addComponent(label_4)
                            .addPreferredGap(ComponentPlacement.RELATED)))
                    .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                        .addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                        .addComponent(textField_4, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                        .addGroup(gl_panel_1.createSequentialGroup()
                            .addComponent(textField, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(ComponentPlacement.RELATED, 13, Short.MAX_VALUE))
                        .addComponent(textField_5, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                        .addComponent(textField_6, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE))
                    .addGap(66))
                .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING, false)
                        .addComponent(label_6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(label_2, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(10, Short.MAX_VALUE))
                .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
                    .addGap(52)
                    .addComponent(button)
                    .addContainerGap(91, Short.MAX_VALUE))
        );
        gl_panel_1.setVerticalGroup(
            gl_panel_1.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel_1.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addComponent(button, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
                    .addGap(18)
                    .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                            .addGap(18)
                            .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                .addGroup(gl_panel_1.createSequentialGroup()
                                    .addGap(40)
                                    .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(textField_5, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
                            .addPreferredGap(ComponentPlacement.UNRELATED)
                            .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label_1)))
                        .addGroup(gl_panel_1.createSequentialGroup()
                            .addGap(206)
                            .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                    .addGap(33)
                    .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                    .addGap(40))
        );
        panel_1.setLayout(gl_panel_1);
        
        button.addActionListener(new ActionListener(){//开始答题
            public void actionPerformed(ActionEvent e)
            {
                count=1;
                T=0;F=0;
                str="";
                z="";
                stop=false;
                textField_6.setText("");
                textField_4.setText(""+count);
                try{
                num=Integer.parseInt(textField.getText());
                if(num>5)
                {
                    
                    textPane.setText("输入题数错误,请重新输入!(1~5)");
                    textField.setText("");
                }
                else
                {
                    startTime=System.currentTimeMillis();
                    textField_1.setText(""+T);
                    textField_2.setText(""+F);
                    button.setVisible(false);
                    textField.setEditable(false);
                    showtext();
                }
                }
                catch(NumberFormatException e1)
                {
                    textPane.setText("输入题数错误,请重新输入!(1~5)");
                    textField.setText("");
                }
                
                
            }            
        });
        textField_5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {try{
                if(!stop)
                {
                
                
                uanswer=Integer.parseInt(textField_5.getText());
                if(num==count)
                {
                    if(uanswer==ranswer) 
                    {  
                        textField_6.setText(""+ranswer);
                        textField_3.setText("答对!"); 
                        T++; 
                        textField_1.setText(""+T);    
                        count++;    
                    }  
                    else 
                    {  
                        textField_6.setText(""+ranswer);
                        textField_3.setText("答错!");
                        F++;
                        textField_2.setText(""+F);
                        count++;
                    }
                    stop=true;
                    textField_3.setText(" 答題結束!");
                    endTime=System.currentTimeMillis();
                    textField.setEditable(true);
                    button.setVisible(true);
                    textField_7.setText((endTime-startTime)/1000+"s");
                    
                }
                else if(uanswer==ranswer) 
                {   
                    textField_6.setText(""+ranswer);
                    textField_3.setText("答对!"); 
                    T++; 
                    textField_1.setText(""+T);    
                    showtext();
                    count++;
                    
                }  
                else
                {   
                    textField_6.setText(""+ranswer);
                    textField_3.setText("答错!");
                    F++;
                    textField_2.setText(""+F);
                    showtext();
                    count++;
                }
                }
            }
                
                catch(NumberFormatException e2)
                {
                    textPane.setText(str+"答案格式错误,请重新输入答案!");
                    textField_5.setText("");
                }
            
            }
        });
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                    .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
                    .addGap(28))
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                    .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                        .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 484, GroupLayout.PREFERRED_SIZE)
                        .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panel.setLayout(gl_panel);
        frame.getContentPane().setLayout(groupLayout);
        frame.setBounds(100, 100, 454, 518);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }    
    void showtext()
    {
        char op='+';
        int a=0,b=0,c=0,e=0,f=0,opnum=0,chioce=0;
        int temp=0;
        Random ra =new Random();
        e=ra.nextInt(10)+1;
        f=ra.nextInt(10)+1;
        chioce=ra.nextInt(2)+1;
        if(e%2==0)
        {
        a=ra.nextInt(10)+2;
        }
        else
        {
            a=-ra.nextInt(10)+2;
        }
        if(f%2==0)
        {
        b=ra.nextInt(10)+2;
        }
        else
        {
            b=-ra.nextInt(10)+2;
        }
        c=ra.nextInt(5);
        opnum=Math.abs(ra.nextInt())%4+1;    
        textField_5.setText("");
        switch(chioce)
        {
        case 1:
        {
        switch(opnum)  
            {  
            case 1:    
                op='+';  
                break;
            case 2:  
                op='-'; 
                break;
            case 3:  
                op='*';    
                break;
            case 4:  
                op='/';  
                break; 
            }                                               
                                                            
            if((op=='-') && (a<b)) 
            {   
                temp=a; 
                a=b;
                b=temp;  
            }
                                                          
            if(op=='/')  
            {   
                if(b==0)
                {    
                    b=1;  
                }   
                a=a*b; 
            }  
            if(a>0&&b>0)
            {
                z=String.valueOf(a)+String.valueOf(op)+String.valueOf(b)+"="+"\n";
            }
            else if(a<0&&b>0)
            {
                z="("+String.valueOf(a)+")"+String.valueOf(op)+String.valueOf(b)+"="+"\n";
            }
            else if(a>0&&b<0)
            {
                z=String.valueOf(a)+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"\n";
            }
            else
            {
                z="("+String.valueOf(a)+")"+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"\n";
            }
            str=str+z;                                                  
            textPane.setText(str); 
        
            switch(op) 
            {  
            case '+':   ranswer=a+b;   break;
            case '-':   ranswer=a-b;   break;
            case '*':   ranswer=a*b;   break;
            case '/':   ranswer=a/b;   break;  
            } 
            break;
        }
        case 2:
        {
            int jc=1;
            str=str+String.valueOf(c)+"!="+"\n";                                                
            textPane.setText(str);
            for(int i=c;i>0;i--)
            {
                jc=jc*i;
            }
            ranswer=jc;
            break;
        }
        }
    }
}

 

程序运行截图如下:

===一开始的进去的界面
结对作业(1.0版)(bug1已修复)_第1张图片

 

===程序设定的是输入1~5的题数,如果输入不正确会提示重新输入~~

 

结对作业(1.0版)(bug1已修复)_第2张图片

 

===输入正确后按开始答题按钮,按钮会消失,并且输入题数的框是不可改的。用户只能在输入答案的框内输入,当题数答完后,输入题数的框可以重新输入,开始答题的按钮会重新出现~~~

 

结对作业(1.0版)(bug1已修复)_第3张图片

 

===每答完一题,界面会显示正确答案和判断对错,并且显示答对的题数和答错的题数,答题结束后,界面显示用户答题的所用时间,然后用户可以重新输入答题的题数,重新开始或者按退出按钮退出~~~

 

结对作业(1.0版)(bug1已修复)_第4张图片

 

===作业中的照片,人太丑,不敢露脸~~~

 

结对作业(1.0版)(bug1已修复)_第5张图片

 

 这个结对作业,我们估计的总用时是8-10小时,结果我们用了7个小时完成了这个实验,比预计的快了一个小时。

1.我们用的是Eclipse(java)进行编程的

2.我们完成的内容是:  

    (2).可以出表达式里含有负整数(负整数最小不小于-100)的题目,且负数需要带括号,用户输入的结果不用带括号。如: 2*(-4) = -8  

    (3)用户答题结束以后,程序可以显示用户答题所用的时间  

    (4)用户可以选择出题的个数(最多不能超过5个题目),答题结束可以显示用户答错的题目个数和答对的题目个数  

    (6)程序可以出单个整数阶乘的题目:如:4!=24

3.本次结对的同学为 :

                   201306114305 欧其锋  博客:http://www.cnblogs.com/ouqifeng/

                   201306114305 吕日荣  博客:http://www.cnblogs.com/Russelling/

4.这个编程过程中,框架界面是共同完成的,欧其锋完成的功能是(2)(3),吕日荣完成的功能是(4)(6);

 

总结:通过这次实验,我觉得我们的收获还是挺大的,首先这个实验要求我们能够提高效率,因为结对的作用就是为了提高工作的效率,这方面我们还是做到了,我们用的时间比预想的时间少了。其次这个实验其实最难的部分是界面的实现,做界面这个东西,我们运用到了WindowBuilder,这个东西让我们在做界面的时候提高了效率,也提高了容错率,在这方面我觉得这个东西帮到了我们很多。最后就是功能与面板之间的一个支持方面的难点,我们在这方面使用了很多很多时间去解决这个问题。    我们觉得,这个实验其实只是一个开始,毕竟我们寻找bug的能力还有所缺陷,我们想到的范围还是比较狭隘的,我们希望这个实验是我们提高的一个阶段,当老师寻找到bug的时候,我们会继续去思考,解决这些bug,完善这个程序。   

     这只是1.0的版本,后期会有功能更加完善的功能的2.0,3.0版,请期待!

 

bug1已经修改好了,代码更新了,解决方法是将NumberFormatException加入一个异常处理,除了输入题数的框修改外,输入答案的框也进行了异常处理;

修复后的截图如下:

结对作业(1.0版)(bug1已修复)_第6张图片

结对作业(1.0版)(bug1已修复)_第7张图片

结对作业(1.0版)(bug1已修复)_第8张图片

你可能感兴趣的:(bug)