實驗二 結對程式設計

09Chaney發表於2015-04-09
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哥。

 

相關文章