zoukankan      html  css  js  c++  java
  • 实验二 结对编程

    package 四则运算;
    
    import java.*;
    import java.util.*;
    
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class MyFrame extends JFrame
    {
        JLabel L1=new JLabel("请输入题数(最多不超过5题):");
        JLabel L2=new JLabel("第一题:");
        JLabel L3=new JLabel("   =   ");
        JLabel L4=new JLabel("第二题:");
        JLabel L5=new JLabel("   =   ");
        JLabel L6=new JLabel("第三题:");
        JLabel L7=new JLabel("   =   ");
        JLabel L8=new JLabel("第四题:");
        JLabel L9=new JLabel("   =   ");
        JLabel L10=new JLabel("第五题:");
        JLabel L11=new JLabel("   =   ");
        JLabel L12=new JLabel("欢迎您,尊敬的 ");
        JLabel L13=new JLabel("用户");
        JLabel L14=new JLabel("");
        JLabel L15=new JLabel("           ");
        JLabel L16=new JLabel("   ");
        JLabel L17=new JLabel("   ");
        JLabel L18=new JLabel("");
        JLabel L19=new JLabel("                               ");
        JTextField[]T={
                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),new JTextField(5)
            };
        JButton B1=new JButton("开始答题");
        JButton B2=new JButton("提交");
        
        JFrame f=new JFrame();
        
        String username=new String();
        int i;
        int k;
        int n;
        long begintime;
        long endtime;
        long time;
        int[]a=new int[5];
        int[]b=new int[5];
        int[]c=new int[5];
        int[]d=new int[5];
        int[]answer=new int[5];
        
        
        public MyFrame() 
        {
            getContentPane().setBackground(Color.WHITE);
            this.setVisible(true);
            this.setTitle("四则运算答题");
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((d.width - 502) / 2, (d.height - 450) / 2, 502, 450);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false);
            getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 12, 10));
            L12.setForeground(Color.RED);
            L12.setFont(new Font("宋体", Font.BOLD, 25));
            getContentPane().add(L12);
            L13.setForeground(SystemColor.textHighlight);
            L13.setFont(new Font("宋体", Font.BOLD, 25));
            getContentPane().add(L13);
            L1.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L1);
            T[10].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[10]);
            B1.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(B1);
            L2.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L2);
            T[0].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[0]);
            L3.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L3);
            T[5].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[5]);
            L4.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L4);
            T[1].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[1]);
            L5.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L5);
            T[6].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[6]);
            L6.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L6);
            T[2].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[2]);
            L7.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L7);
            T[7].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[7]);
            L8.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L8);
            T[3].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[3]);
            L9.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L9);
            T[8].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[8]);
            L10.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L10);
            T[4].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[4]);
            L11.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L11);
            T[9].setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(T[9]);
            L14.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L14);
            L19.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L19);
            B2.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(B2);
            L15.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L15);
            L16.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L16);
            L14.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L14);
            L17.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L17);
            L18.setFont(new Font("宋体", Font.PLAIN, 20));
            getContentPane().add(L18);
            
            File file=new File("用户名.txt");
            if(!file.exists())
            {
                try
                 {
                    file.createNewFile();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                Inputname b=new Inputname();
            }
            else
            {
                String str1=new String("");; 
                int i=0;
                BufferedReader br = null;
                try 
                {
                    br = new BufferedReader(new FileReader(file.getName()));
                } 
                catch (FileNotFoundException e1) 
                {
                    e1.printStackTrace();
                }
                try 
                {
                    while((str1=br.readLine())!=null)
                    {
                        username=str1;
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                try 
                {
                    br.close();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
                if(username.length()==0)
                {
                    Inputname b=new Inputname();
                }
                else
                {
                    L13.setText(username);
                }
            }
            
            B1.addActionListener(new Action());
            B2.addActionListener(new Action());
        }
        
        public class Action implements ActionListener
        {
    
            public void actionPerformed(ActionEvent e) 
            {
                try
                {
                    if(e.getSource()==B1)
                    {
                        for(k=0;k<10;k++)
                        {
                            T[k].setText(null);
                        }
                        L15.setText("           ");
                        L16.setText(null);
                        L17.setText(null);
                        L18.setText("");
                        i=0;
                        
                        if(T[10].getText().length()==0)
                        {
                            JOptionPane.showMessageDialog(f,"请输入题数!");
                        }
                        else if(Integer.parseInt(T[10].getText())>5)
                        {
                            JOptionPane.showMessageDialog(f,"题数最多不超过5!");
                        }
                        else
                        {
                            i=Integer.parseInt(T[10].getText());
                            
                            for(k=0;k<i;k++)
                            {
                                a[k]=(int)(Math.random()*100+1);
                                b[k]=(int)(Math.random()*100+1);
                                c[k]=(int)(Math.random()*5);
                                d[k]=(int)(Math.random()*12+1);
                                if(c[k]==0)
                                {
                                    T[k].setText(a[k]+"+"+b[k]);
                                    answer[k]=a[k]+b[k];
                                }
                                if(c[k]==1)
                                {
                                    T[k].setText(a[k]+"-"+b[k]);
                                    answer[k]=a[k]-b[k];
                                }
                                if(c[k]==2)
                                {
                                    T[k].setText(a[k]+"*"+b[k]);
                                    answer[k]=a[k]*b[k];
                                }
                                if(c[k]==3)
                                {
                                    T[k].setText(a[k]+"/"+b[k]);
                                    answer[k]=a[k]/b[k];
                                }
                                if(c[k]==4)
                                {
                                    T[k].setText(d[k]+"!");
                                    answer[k]=jiecheng(d[k]);
                                }
                            }
                            begintime = System.currentTimeMillis();
                        }
                    }
                }catch(NumberFormatException u)
                {
                    JOptionPane.showMessageDialog(f,"请输入正确的数字!");
                }
                if(e.getSource()==B2)
                {
                    if(i==0)
                    {
                        JOptionPane.showMessageDialog(f,"请先进行答题!");
                    }
                    else
                    {
                        endtime = System.currentTimeMillis();
                        time=(endtime-begintime)/1000;
                        n=0;
                        for(k=0;k<i;k++)
                        {
                            if(T[k+5].getText().length()==0)
                            {
                                continue;
                            }
                            try
                            {
                                if(answer[k]==Integer.parseInt(T[k+5].getText()))
                                {
                                    n++;
                                }
                            }catch(NumberFormatException u)
                            {
                                JOptionPane.showMessageDialog(f,"请输入正确的数字!");
                            }
                            
                        }
                        L15.setText("你答对了:"+n+"道题");
                        L16.setText("你答错了:"+(i-n)+"道题");
                        L14.setText("用时:"+time+"秒");
                        L17.setText("参考答案:");
                        for(k=0;k<i;k++)
                        {
                            L18.setText(L18.getText()+answer[k]+";");
                        }
                        i=0;
                    }
                }
            }
            
            int jiecheng(int x)
            {
                if(x==0 || x==1)
                {
                    return 1;
                }
                else
                {
                    return x*jiecheng(x-1);
                }
            }
            
        }
        
        public class Inputname extends JFrame
        {
            JLabel L1=new JLabel("请输入您的用户名:");
            JTextField T1=new JTextField(17);
            JButton B1=new JButton("确定");
            JButton B2=new JButton("关闭");
            
            JFrame f=new JFrame();
            
            public Inputname()
            {
                this.setVisible(true);
                this.setTitle("输入用户名");
                Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
                setBounds((d.width - 241) / 2, (d.height - 138) / 2, 242, 146);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setResizable(false);
                getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 20, 6));
                L1.setFont(new Font("宋体", Font.PLAIN, 20));
                getContentPane().add(L1);
                T1.setFont(new Font("宋体", Font.PLAIN, 20));
                getContentPane().add(T1);
                B1.setFont(new Font("宋体", Font.PLAIN, 20));
                getContentPane().add(B1);
                B2.setFont(new Font("宋体", Font.PLAIN, 20));
                getContentPane().add(B2);
                B1.addActionListener(new action());
                B2.addActionListener(new action());
            }
            
            public class action implements ActionListener
            {
    
                public void actionPerformed(ActionEvent e)
                {
                    if(e.getSource()==B2)
                    {
                        setVisible(false);
                    }
                    
                    if(e.getSource()==B1)
                    {
                        if(T1.getText().length()==0)
                        {
                            JOptionPane.showMessageDialog(f,"用户名不能为空");
                        }
                        else
                        {
                            username=T1.getText();
                            L13.setText(username);
                            
                            File file=new File("用户名.txt");
                            try{
                            BufferedWriter writer=new BufferedWriter(new FileWriter(file.getName()));
                                writer.write(username);;
                            writer.close();
                            } catch(IOException g)
                            {
                                System.out.print(g);
                            }
                            
                            setVisible(false);
                        }
                        
                    }
                    
                }
                
            }
    
        }
        
    }
    View Code
    package 四则运算;
    
    public class Test {
    
        public static void main(String[] args) {
            
            MyFrame a=new MyFrame();
    
        }
    
    }
    View Code

        先来个粗暴直接地上代码;然后是时候介绍我的帅气队友许佳豪http://www.cnblogs.com/xujiahao/

    最后上图 (图右一为队友),至于在宿舍工作的图片就不曝出来了,惨不忍睹。

           关于这次结对的心得:一个字,爽。以前没尝试过这样的合作方式,认真的体验了一把团体合作。这次我担任领航员负责用插件弄了界面的工作还有跟队友一起探讨设计选择了扩展方向的2,3,4,5,6,剩下队友担任驾驶员与我一起商量实现其功能,历时两个晚上,写出了这个小小程序。这次给我的感觉最大的并不是编程方面的体会,而是两个人之间一起商量讨论甚至争论的过程,感谢队友,人称av哥。

  • 相关阅读:
    FJSC2020合集
    考试前注意事项
    CSP-S&&NOIP2020游记
    IOI2020集训队作业题单
    对拍程序
    Re:memset 赋值
    2019-12-29 Div.3模拟赛题解
    NOI2020 游记
    边三连通分量算法
    【题解】Code+7 教科书般的亵渎
  • 原文地址:https://www.cnblogs.com/chaney/p/4409782.html
Copyright © 2011-2022 走看看