練習2-小學生四則運算系統

Kaci發表於2015-04-09

---恢復內容開始---

開發環境:  Eclipse

題目方向:  一個能自動生成小學四則運算題目的程式,一個單機帶使用者介面(不是控制檯)的程式。

題目要求:  

  • 生成題目,單個題目最多不能超過4個運算子,運算元小於100。
  • 使用者可以輸入答案
  • 若使用者輸入答案正確,則提示正確;若答案錯誤,則提示錯誤,並要提示正確答案是多少。

結對子用時:  3天3夜+1個下午

題目擴充套件方向:

 

  • 程式設定了登入功能,新使用者第一次使用程式時可以進行註冊,舊使用者使用程式時可以根據歷史記錄的資訊而登入。
  • 程式可以設定皮膚功能,更改介面的顏色。
  • 使用者可以設定做題的數目,答題結束後可以顯示使用者答題的對錯數目並記錄總分。
  • 表示式裡面可以進行計算負數的運算題目。
  • 程式分別對加法、減法、乘法、除法設定了簡單、適中和困難這3中程度的題目。
  • 程式出錯時,系統會反饋出錯誤的資訊。

 

結對夥伴:

姓名 學號 部落格地址
方俊傑 201306114417 http://www.cnblogs.com/imfjj/
餘雅詩 201306114453 http://www.cnblogs.com/ys1101/

源程式:  

 

package opar;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JTextField;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class Login_Fail implements ActionListener{

    private JFrame frame;
    private JButton button;
    private JTextField textField;



    public Login_Fail() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setTitle("\u63D0\u793A");
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Login_Fail.class.getResource("/opar/\u6807\u7B7E.jpg")));
        Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(200, 200 );
        frame.setLocation((screenSize.width-200 )/2, (screenSize.height-200)/2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        
        textField = new JTextField();
        textField.setHorizontalAlignment(SwingConstants.CENTER);
        textField.setEditable(false);
        textField.setText("\u8D26\u6237\u4E0D\u5B58\u5728\u6216\u5BC6\u7801\u9519\u8BEF");
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        
        button = new JButton("\u91CD\u65B0\u767B\u5F55");
        frame.getContentPane().add(button);
        button.addActionListener(this);
        
        
        
    
        
    }
    public void Set_Ssetvial(boolean y)
    {
        this.frame.setVisible(y);
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getSource()==this.button)//重新登入
        {
            
            this.frame.setVisible(false);
        
        }
        
        
    }
    public void Set_T(String x)
    {
        this.textField.setText(x);
    }
}

 

package opar;

import java.awt.EventQueue;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

public class Login_Pupil_Enroll implements ActionListener {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_3;
    private JTextField textField_4;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;
    private JButton button_1;
    private JButton button;
    private File Pupil_Path;
    private File Pupil_Information;
    private JPasswordField passwordField_2;

    /**
     * Launch the application.
     */

    /**
     * Create the application.
     * 
     * @throws IOException
     */
    // 構造方法
    public Login_Pupil_Enroll() throws IOException {
        initialize();
        Pupil_Path = new File("D:/Pupil_Path");

        if (!Pupil_Path.exists()) {
            Pupil_Path.mkdirs();

        }
        Pupil_Information = new File(Pupil_Path, "小學生使用者.txt");
        Pupil_Information.createNewFile();
    }

    /**
     * Initialize the contents of the frame.
     */
    // 初始化方法
    private void initialize() {
        frame = new JFrame();
        frame.setTitle("\u65B0\u7528\u6237\u6CE8\u518C");
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Login_Pupil_Enroll.class.getResource("/opar/\u6807\u7B7E.jpg")));
        frame.getContentPane().setBackground(new Color(176, 224, 230));
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(400, 400);
        frame.setLocation((screenSize.width - 400) / 2,
                (screenSize.height - 400) / 2);
        frame.getContentPane().setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBackground(new Color(224, 255, 255));

        JPanel panel_1 = new JPanel();
        panel_1.setToolTipText("\u65B0\u7528\u6237\u6CE8\u518C");
        panel_1.setBackground(new Color(224, 255, 255));
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                        .addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
                    .addGap(53))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
                    .addContainerGap())
        );
        panel_1.setLayout(null);

        JLabel label = new JLabel("\u7528\u6237\u6635\u79F0\uFF1A");
        label.setBounds(23, 28, 72, 15);
        panel_1.add(label);

        JLabel label_1 = new JLabel("\u7528\u6237\u5BC6\u7801\uFF1A");
        label_1.setBounds(23, 53, 72, 15);
        panel_1.add(label_1);

        JLabel label_2 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
        label_2.setBounds(23, 78, 72, 15);
        panel_1.add(label_2);

        textField = new JTextField();
        textField.setBounds(94, 28, 172, 21);
        panel_1.add(textField);
        textField.setColumns(10);

        textField_3 = new JTextField();
        textField_3.setEditable(false);
        textField_3.setBounds(94, 174, 172, 21);
        panel_1.add(textField_3);
        textField_3.setColumns(10);

        JLabel label_3 = new JLabel("\u53CD\u9988\u6846\uFF1A");
        label_3.setBounds(33, 174, 72, 15);
        panel_1.add(label_3);

        textField_4 = new JTextField();
        textField_4.setBounds(94, 109, 172, 21);
        panel_1.add(textField_4);
        textField_4.setColumns(10);

        JLabel label_4 = new JLabel("\u8EAB\u4EFD\u8BC1\u53F7\uFF1A");
        label_4.setBounds(23, 103, 72, 27);
        panel_1.add(label_4);

        passwordField = new JPasswordField();
        passwordField.setBounds(94, 53, 172, 21);
        panel_1.add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(94, 78, 173, 21);
        panel_1.add(passwordField_1);

        button = new JButton("\u786E\u8BA4");
        button.setBounds(63, 221, 93, 23);
        panel_1.add(button);
        button.addActionListener(this);

        button_1 = new JButton("\u8FD4\u56DE");
        button_1.setBounds(210, 221, 93, 23);
        panel_1.add(button_1);

        JLabel label_5 = new JLabel("\u786E\u8BA4\u4E00\u6B21\uFF1A");
        label_5.setBounds(23, 140, 72, 24);
        panel_1.add(label_5);

        passwordField_2 = new JPasswordField();
        passwordField_2.setBounds(94, 143, 172, 21);
        panel_1.add(passwordField_2);
        
        JLabel label_6 = new JLabel("*/6-12\u4E2A\u5B57\u7B26");
        label_6.setBounds(280, 28, 75, 15);
        panel_1.add(label_6);
        
        JLabel label_7 = new JLabel("*/6\u4E2A\u5B57\u7B26");
        label_7.setBounds(280, 53, 75, 15);
        panel_1.add(label_7);
        
        JLabel label_8 = new JLabel("*/6\u4E2A\u5B57\u7B26");
        label_8.setBounds(280, 78, 75, 15);
        panel_1.add(label_8);
        
        JLabel label_9 = new JLabel("*/18\u4E2A\u6570\u5B57");
        label_9.setBounds(280, 109, 75, 15);
        panel_1.add(label_9);
        
        JLabel label_10 = new JLabel("*/18\u4E2A\u6570\u5B57");
        label_10.setBounds(280, 146, 75, 15);
        panel_1.add(label_10);
        
        JLabel label_11 = new JLabel("*/\u6CE8\u518C\u52A8\u6001");
        label_11.setToolTipText("\u6CE8\u518C\u52A8\u6001");
        label_11.setBounds(280, 174, 75, 15);
        panel_1.add(label_11);
        panel.setLayout(null);
        button_1.addActionListener(this);

        JLabel lblNewLabel = new JLabel("\u7528\u6237\u6CE8\u518C\u4E2D");
        lblNewLabel.setBackground(Color.CYAN);
        lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 30));
        lblNewLabel.setBounds(105, 10, 182, 42);
        panel.add(lblNewLabel);
        frame.getContentPane().setLayout(groupLayout);

    }

    public void Set_Setvial(boolean y) {
        this.frame.setVisible(y);
    }

    @Override
    // 對事件處理介面的方法
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if (arg0.getSource() == this.button) {
            try {
                FileWriter filew = new FileWriter(Pupil_Information, true);
                if (this.textField.getText().length() <= 12
                        && this.textField.getText().length() >= 6) {
                    if (this.passwordField.getText().length() == 6
                            && this.passwordField.getText().length() == 6) {
                        if (this.passwordField.getText().hashCode() == this.passwordField_1
                                .getText().hashCode()) {
                            if (this.textField_4.getText().matches("[0-9]*")
                                    && this.passwordField_2.getText().matches(
                                            "[0-9]*")) {
                                if (this.textField_4.getText().length() == 18
                                        && this.passwordField_2.getText()
                                                .length() == 18) {
                                    if (this.textField_4.getText().hashCode() == this.passwordField_2
                                            .getText().hashCode()) {
                                        filew.write(this.textField.getText()
                                                + "\t");
                                        filew.write(this.passwordField
                                                .getText() + "\t");
                                        filew.write(this.textField_4.getText()
                                                + "\t\r\n");
                                        
                                        filew.flush() ; //關閉前重新整理
                                        filew.close();
                                        this.textField_3.setText(null);
                                        this.textField_3.setText("註冊成功!");
                                        
                                        

                                    } else {
                                        this.textField_3.setText(null);
                                        this.textField_3
                                                .setText(this.textField_3
                                                        .getText() + "身份證號不相同!");
                                    }
                                } else {
                                    this.textField_3.setText(null);
                                    this.textField_3.setText(this.textField_3
                                            .getText() + "身份證號應為18位!");
                                }
                            } else {
                                this.textField_3.setText(null);
                                this.textField_3.setText(this.textField_3
                                        .getText() + "身份證號應為數字!");
                            }

                        } else {
                            this.textField_3.setText(null);
                            this.textField_3.setText(this.textField_3.getText()
                                    + "密碼不相同!");
                        }
                    } else {
                        this.textField_3.setText(null);
                        this.textField_3.setText(this.textField_3.getText()
                                + "密碼不是6位!");
                    }

                } else {
                    this.textField_3.setText(null);
                    this.textField_3.setText(this.textField_3.getText()
                            + "不是6~12個字元");
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (arg0.getSource() == this.button_1) {
            this.frame.setVisible(false);
            Login l = new Login();
            l.Set_frame(true);

        }

    }
}
package opar;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.awt.SystemColor;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

class Login implements ActionListener ,KeyListener{

    private JFrame frame;
    private JPasswordField passwordField;
    private JTextField textField;
    private JButton button;// 確定
    private JButton button_1;// 重置
    private JButton btnNewButton;// 退出
    private JButton btnNewButton_1;// 註冊
    private T t;;
    private Login_Fail lf;;
    private Login_Pupil_Enroll l_p_e;
    private File Pupil_Path;
    private File Pupil_Information;
    private FileReader filer;
    private BufferedReader br;
    private String b;
    private static int count = 0;
    private Pupil_Information_save[] Pupil = new Pupil_Information_save[100];
    private static int fig = 0;

    public Login() //
    {
        initialize();
    }

    public void Set_frame(boolean y) // 設定可視性
    {
        frame.setVisible(y);
    }

    private void initialize() {
        frame = new JFrame();
        frame.setTitle("\u65B9\u4FCA\u6770-\u4F59\u96C5\u8BD7");
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Login.class.getResource("/opar/\u6807\u7B7E.jpg")));
        frame.getContentPane().setBackground(SystemColor.inactiveCaption);
        frame.setSize(500, 500);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((screenSize.width - 500) / 2,
                (screenSize.height - 500) / 2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBackground(Color.PINK);

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(Color.PINK);

        JPanel panel_2 = new JPanel();
        panel_2.setBackground(Color.PINK);
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(panel_2, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                        .addComponent(panel_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                        .addComponent(panel, GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE))
                    .addContainerGap())
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addContainerGap())
        );

        panel_2.setLayout(null);
        
                btnNewButton = new JButton("");
                btnNewButton.setBackground(Color.PINK);
                btnNewButton.setToolTipText("");
                btnNewButton.setBounds(0, 0, 464, 119);
                panel_2.add(btnNewButton);
                btnNewButton.setIcon(new ImageIcon(Login.class.getResource("/opar/exit.png")));
                btnNewButton.addActionListener(this);
        panel_1.setLayout(null);

        JLabel label = new JLabel("\u7528\u6237\u540D\uFF1A");
        label.setFont(new Font("宋體", Font.PLAIN, 15));
        label.setBounds(57, 41, 66, 15);
        panel_1.add(label);

        JLabel label_1 = new JLabel("\u5BC6\u7801\uFF1A");
        label_1.setFont(new Font("宋體", Font.PLAIN, 15));
        label_1.setBounds(57, 87, 66, 15);
        panel_1.add(label_1);

        passwordField = new JPasswordField();
        passwordField.setBounds(133, 84, 237, 21);
        panel_1.add(passwordField);

        textField = new JTextField();
        textField.setBounds(133, 38, 237, 21);
        panel_1.add(textField);
        textField.setColumns(10);

        button = new JButton("\u767B\u5F55");
        button.setBounds(74, 134, 93, 23);
        panel_1.add(button);
        button.addActionListener(this);
        button.addKeyListener(this);

        button_1 = new JButton("\u91CD\u7F6E");
        button_1.setBounds(189, 134, 93, 23);
        panel_1.add(button_1);
        button_1.addActionListener(this);

        JLabel label_2 = new JLabel("^-^ \u52A0\u6CB9 ^-^");
        label_2.setBounds(202, 183, 77, 15);
        panel_1.add(label_2);

        btnNewButton_1 = new JButton("\u6CE8\u518C");
        btnNewButton_1.setBounds(306, 134, 86, 23);
        panel_1.add(btnNewButton_1);
        panel.setLayout(null);
        btnNewButton_1.addActionListener(this);

        JLabel lblNewLabel = new JLabel(
                "\u6B22\u8FCE\u767B\u5F55\u5C0F\u5B66\u751F\u56DB\u5219\u8FD0\u7B97\u7CFB\u7EDF");
        lblNewLabel.setBackground(Color.PINK);
        lblNewLabel.setForeground(SystemColor.inactiveCaptionText);
        lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 30));
        lblNewLabel.setBounds(37, 10, 390, 65);
        panel.add(lblNewLabel);

        t = new T();
        lf = new Login_Fail();
        try {
            l_p_e = new Login_Pupil_Enroll();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.print("不能夠建立小學生使用者登入文件");
        }

        frame.getContentPane().setLayout(groupLayout);
        // frame.setVisible(true);
    }

    JButton Get_button() {
        return button;
    }

    JButton Get_button_1() {
        return button_1;
    }

    JButton Get_btnNewButton_1() {
        return btnNewButton_1;
    }

    JButton Get_btnNewButton() {
        return btnNewButton;
    }

    JPasswordField Get_PasswordField() {
        return passwordField;
    }

    JTextField Get_textField() {
        return textField;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        // 登入按鈕
        if (arg0.getSource() == this.button) {
            Pupil_Path = new File("D:/Pupil_Path");

            if (!Pupil_Path.exists()) {
                Pupil_Path.mkdirs();

            }
            Pupil_Information = new File(Pupil_Path, "小學生使用者.txt");
            try {
                Pupil_Information.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                filer = new FileReader(Pupil_Information);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            br = new BufferedReader(filer);

            b = "";
            try {
                while ((b = br.readLine()) != null) {

                    String[] sFirst = b.split("\t");// System.out.print(b);
                    Pupil[count] = new Pupil_Information_save(sFirst[0],
                            sFirst[1], sFirst[2]);

                    count++;

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           fig=0;//標記初始化
            for (int i = 0; i < count; i++) {

                if (this.textField.getText().hashCode() == Pupil[i].Get_Name()
                        .hashCode()) {fig = 1;
                    if (this.passwordField.getText().hashCode() == Pupil[i]
                            .Get_Code().hashCode()) {
                        t.Set_Setvial(true);
                        this.Set_frame(false);
                        fig=2;
                        

                    }
                    

                }
            

            }

            if (fig == 0)
            {

                lf.Set_T("賬戶不存在!");
                lf.Set_Ssetvial(true);
            }
            else if (fig == 1) {
    
                lf.Set_T("密碼錯誤!");
                lf.Set_Ssetvial(true);

            }

        }

        // 重置按鈕
        else if (arg0.getSource() == this.button_1) {
            this.textField.setText(null);
            this.passwordField.setText(null);
        }
        // 註冊按鈕
        else if (arg0.getSource() == this.btnNewButton_1) {
            this.Set_frame(false);
            l_p_e.Set_Setvial(true);
        } else if (arg0.getSource() == this.btnNewButton) {
            System.exit(0);
        }

    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getKeyCode()==KeyEvent.VK_ENTER)
        {
            System.out.print("ssss");
            Pupil_Path = new File("D:/Pupil_Path");

            if (!Pupil_Path.exists()) {
                Pupil_Path.mkdirs();

            }
            Pupil_Information = new File(Pupil_Path, "小學生使用者.txt");
            try {
                Pupil_Information.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                filer = new FileReader(Pupil_Information);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            br = new BufferedReader(filer);

            b = "";
            try {
                while ((b = br.readLine()) != null) {

                    String[] sFirst = b.split("\t");// System.out.print(b);
                    Pupil[count] = new Pupil_Information_save(sFirst[0],
                            sFirst[1], sFirst[2]);

                    count++;

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           fig=0;//標記初始化
            for (int i = 0; i < count; i++) {

                if (this.textField.getText().hashCode() == Pupil[i].Get_Name()
                        .hashCode()) {fig = 1;
                    if (this.passwordField.getText().hashCode() == Pupil[i]
                            .Get_Code().hashCode()) {
                        t.Set_Setvial(true);
                        this.Set_frame(false);
                        fig=2;
                        

                    }
                    

                }
            

            }

            if (fig == 0)
            {

                lf.Set_T("賬戶不存在!");
                lf.Set_Ssetvial(true);
            }
            else if (fig == 1) {
    
                lf.Set_T("密碼錯誤!");
                lf.Set_Ssetvial(true);

            }

        }
        
        
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}
package opar;

public class Pupil_Information_save {
    private String Name;//暱稱
    private String Code;//密碼
    private String ID_card_No;//身份證號碼
    public Pupil_Information_save(String a,String b,String c)
    {
        this.Name=a;
        this.Code=b;
        this.ID_card_No=c;
    }
    public String Get_Name()
    {
        return this.Name;
    }
    public String Get_Code()
    {
        return this.Code;
    }
    String Get_ID_card_No()
    {
        return ID_card_No;
    }

}
package opar;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class T_Event implements ActionListener, KeyListener {

    static int t = 0;
    static int f = 0;
    static int con = 1;// 預設適中
    static int del = 0;
    private static int sum=1000;
    private static int times=0;
    void Set_t(int t) {
        this.t = this.t + 1;
    }

    void Set_f(int f) {
        this.f = this.f + 1;
    }

    void Set_del() {
        while (new T().Get_T2() == 0 || new T().Get_T() % new T().Get_T2() != 0) {
            if (con == 0)
                new T().Set_rom_rom1(0, 20);

            else if (con == 1)
                new T().Set_rom_rom1(11, 500);

            else if (con == 2)
                new T().Set_rom_rom1(501, 10000);

            new T().set_T_T1_T2_T3();

        }
    }

    int Get_t() {
        return t;
    }

    int Get_f() {
        return f;
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        if (e.getSource() == new T().button_1)// 加法
        {
            del = 0;
            new T().Set_T1("+");
        }
        if (e.getSource() == new T().button_2)// 減法
        {
            del = 0;
            new T().Set_T1("-");
        }
        if (e.getSource() == new T().button_3)// 乘法
        {
            del = 0;
            new T().Set_T1("*");
        }
        if (e.getSource() == new T().btnNewButton_3)// 除法
        {
            this.Set_del();
            del = 1;
            new T().Set_T1("/");

        }
        if(e.getSource()==new T().Get_btnNewButton_4())
        {
            this.t=0;//初始化
            this.f=0;//初始化
            this.times=0;
            
            //new T().Get_T12().setText();
            this.sum=10000;
            new T().set_T_T1_T2_T3();
            
            
            new T().Set_T_T1_T2_T3(true);
        }
        if (e.getSource() == new T().Get_btnNewButton())// 簡單
        {
            new T().Set_rom_rom1(0, 20);
            new T().set_T_T1_T2_T3();
            con = 0;
            if (del == 1)
                this.Set_del();

        }
        if (e.getSource() == new T().Get_btnNewButton_1())// 適中
        {
            new T().Set_rom_rom1(11, 500);
            new T().set_T_T1_T2_T3();
            con = 1;
            if (del == 1)
                this.Set_del();

        }
        if (e.getSource() == new T().Get_btnNewButton_2())// 困難
        {
            new T().Set_rom_rom1(501, 10000);
            new T().set_T_T1_T2_T3();
            con = 2;
            if (del == 1)
                this.Set_del();

        }
        if (e.getSource() == new T().button)
            System.exit(0);
        if (e.getSource() == new T().Get_Com()) {
            int index = new T().Get_Com().getSelectedIndex();
            switch (index) {
            case 0:

                new T().Get_frame().getContentPane().setBackground(Color.RED);
                break;
            case 1:
                new T().Get_frame().getContentPane()
                        .setBackground(Color.orange);
                break;
            case 2:
                new T().Get_frame().getContentPane()
                        .setBackground(Color.yellow);
                break;
            case 3:
                new T().Get_frame().getContentPane().setBackground(Color.green);
                break;
            case 4:
                new T().Get_frame().getContentPane().setBackground(Color.blue);
                break;

            }
        }

    }

    @Override
    public void keyPressed(KeyEvent arg0) {
       //判斷是回車
        
        if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
           if(new T().Get_T12().getText().length()!=0)
               {
               sum=Integer.parseInt(new T().Get_T12().getText());//回車初始化題目數量
               }
           
            // Pattern pattern=Pattern.compile("[0-9]*");!isNum.matches()||
            // Matcher isNum=pattern.matcher(new T().Get_T3());
            if(this.sum>times)
            {
            if (new T().Get_T3().matches("-?[0-9]+.*[0-9]*")) {
                new T().Set_T4_T5_T6_T7();
                if (new T().Get_n() == Integer.parseInt(new T().Get_T3())) {
                    Set_t(t);
                    new T().Set_T9("你很厲害,答對了!");
                }

                else {
                    Set_f(f);
                    new T().Set_T9("你太大意了!答錯了");
                    System.out.print(new T().Get_n()+"    "+Integer.parseInt(new T().Get_T3()));
                }

                new T().Set_T8_T10_T11();
                
                times++;
                
                //加減乘除的
                if (con == 0) {
                    new T().Set_rom_rom1(0, 20);
                    this.Set_del();
                } else if (con == 1) {
                    new T().Set_rom_rom1(21, 500);
                    this.Set_del();
                } else if (con == 2) {
                    new T().Set_rom_rom1(501, 10000);
                    this.Set_del();
                }
                
                
                
                
                new T().set_T_T1_T2_T3();
                
                if (del == 1)
                    this.Set_del();
            } else {
                new T().Set_T9("你輸入的 “ " + new T().Get_T3() + " ”是非法數值");
                new T().Set_T3();

            }
            
            
            }
            else
            {
                new T().Set_T9(new T().Get_T9().getText()+"你已經完成"+new T().Get_T12().getText()+"道題目");
                new T().Set_T_T1_T2_T3(false);
            }

        }

    }
    
    
    

    public void keyReleased(KeyEvent arg0) {
    }

    public void keyTyped(KeyEvent arg0) {
    }
}
package opar;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;

public class T {
    private static JTextField textField;
    private static JTextField textField_1;
    private static JTextField textField_2;
    private static JTextField textField_3;
    private static JTextField textField_4;
    private static JTextField textField_5;
    private static JTextField textField_6;
    private static JLabel label;
    private static JTextField textField_7;
    private static JLabel label_1;
    private static JLabel label_2;
    private static JLabel label_3;
    private static JTextField textField_8;
    private static JTextField textField_9;
    private static JLabel label_4;
    private static JTextField textField_10;
    private static JTextField textField_11;
    private static JFrame frame;
    static Random r = new Random();
    static JButton button_1;
    static JButton button_2;
    static JButton button_3;
    static JButton btnNewButton_3;
    static JButton btnNewButton;
    static JButton btnNewButton_1;
    static JButton btnNewButton_2;
    static JButton button;
    static int n=0;
    static int rom=r.nextInt(501);
    static int rom1=r.nextInt(501);
    private static JLabel label_7;
    private static JLabel lblNewLabel_1;
    private static Login l;
    private static String []Skin={"紅色","橙色","黃色","綠色","藍色"};
    private static JComboBox comboBox ;
    private static String []Number={" 整數","小數"};
    private static JTextField textField_12;
    private static JButton btnNewButton_4;

    void Set_T_T1_T2_T3(boolean y)
    {
        
        this.textField_3.setEditable(y);
    }
    void Set_T3()
    {
        this.textField_3.setText(null);
    }
    
    void Set_Setvial(boolean y)
    {
        frame.setVisible(y);
    }
    void Set_T1(String x)
    {
        textField_1.setText(x);    
        
    }
    
    void Set_T4_T5_T6_T7()
    {
        
        
            textField_4.setText(textField.getText());
            textField_5.setText(textField_1.getText());    
            textField_6.setText(textField_2.getText());
            if(textField_1.getText().equals("+")==true)    
             n=Integer.parseInt(textField.getText())+Integer.parseInt(textField_2.getText());
            else if(textField_1.getText().equals("-")==true)    
                 n=Integer.parseInt(textField.getText())-Integer.parseInt(textField_2.getText());
            else if(textField_1.getText().equals("*")==true)    
                 n=Integer.parseInt(textField.getText())*Integer.parseInt(textField_2.getText());
            else if(textField_1.getText().equals("/")==true)    
                 n=Integer.parseInt(textField.getText())/Integer.parseInt(textField_2.getText());
            textField_7.setText(String.valueOf(n));
            System.out.print("n"+n);
            
    }
    void set_T_T1_T2_T3()
    {
        
        textField.setText(String.valueOf(rom));
        textField_2.setText(String.valueOf(rom1));
        textField_3.setText(null);
    }
    void set_T_T1_T2_T3_(boolean t)
    {
        
        textField.setEditable(t);
        textField_1.setEditable(t);
        this.textField_2.setEditable(t);
        textField_3.setEditable(t);
    }
    void Set_T8_T10_T11()
    {
        textField_8.setText(String.valueOf(new T_Event().Get_t()));
        textField_10.setText(String.valueOf(new T_Event().Get_f()));
        textField_11.setText(    String.valueOf(   new T_Event().Get_t()    *100  / (new T_Event().Get_f()+new T_Event().Get_t()))+"%");
    }
    void Set_rom_rom1(int x1,int x2)
    {
        rom=r.nextInt(x2-x1)+x1;
         rom1=r.nextInt(x2-x1)+x1;
        
    }
    void Set_T9(String s)
    {
         textField_9.setText(s);
    }
    void Set_Login_l(boolean y)
    {
        l.Set_frame(y);
    }
    
    public JComboBox Get_Com()
    {
        return comboBox;
    }
   
    
    String Get_T3()
    {
        return textField_3.getText();
    }
    int Get_n()
    {
        return n;
    }
    int Get_T()
    {
        return Integer.parseInt(textField.getText());
    }
    int Get_T2()
    {
        return Integer.parseInt(textField_2.getText());
    }
    JButton Get_btnNewButton()
    {
        return btnNewButton;
    }
    JButton Get_btnNewButton_1()
    {
        return btnNewButton_1;
    }
    JButton Get_btnNewButton_2()
    {
        return btnNewButton_2;
    }
    JButton Get_button()
    {
        return button;
    }
    public JFrame Get_frame()
    {
        return this.frame;
    }
    public JTextField Get_T12()
    {
        return textField_12;
    }
    public JButton Get_btnNewButton_4()
    {
        return btnNewButton_4;
    }
    public JTextField Get_T9()
    {
        return textField_9;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        frame=new JFrame("小學生四則運算器");
        frame.setTitle("\u5929\u751F\u6211\u6750\u5FC5\u6709\u7528\uFF0C\u52A0\u6CB9\u54E6......");
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage(T.class.getResource("/opar/\u6807\u7B7E.jpg")));
        frame.getContentPane().setBackground(Color.RED);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(500, 500 );
        frame.setLocation((screenSize.width-500 )/2, (screenSize.height-500)/2);
        frame.getContentPane().setLayout(null);
        
        textField = new JTextField(String.valueOf(rom));
        textField.setEditable(false);
        textField.setBounds(96, 152, 66, 21);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        
        textField_1 = new JTextField(" +");
        textField_1.setEditable(false);
        textField_1.setBounds(199, 152, 24, 21);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);
        
        textField_2 = new JTextField(String.valueOf(rom1));
        textField_2.setEditable(false);
        textField_2.setBounds(257, 152, 66, 21);
        frame.getContentPane().add(textField_2);
        textField_2.setColumns(10);
      
        
        JLabel lblNewLabel = new JLabel("=");
    
        lblNewLabel.setBounds(348, 152, 24, 15);
        frame.getContentPane().add(lblNewLabel);
        
        textField_3 = new JTextField();
        textField_3.setBounds(379, 152, 66, 21);
        frame.getContentPane().add(textField_3);
        textField_3.setColumns(10);
        textField_3.addKeyListener(new T_Event());
        
        textField_4 = new JTextField(textField.getText());
        textField_4.setEditable(false);
        textField_4.setBounds(96, 198, 66, 21);
        frame.getContentPane().add(textField_4);
        textField_4.setColumns(10);
        
        textField_5 = new JTextField(" +");
        textField_5.setEditable(false);
        textField_5.setBounds(199, 198, 24, 21);
        frame.getContentPane().add(textField_5);
        textField_5.setColumns(10);
        
        textField_6 = new JTextField(textField_2.getText());
        textField_6.setEditable(false);
        textField_6.setBounds(257, 198, 66, 21);
        frame.getContentPane().add(textField_6);
        textField_6.setColumns(10);
        
        label = new JLabel("=");
       
        label.setBounds(348, 198, 24, 15);
        frame.getContentPane().add(label);
        
        textField_7 = new JTextField();
        textField_7.setEditable(false);
        textField_7.setBounds(379, 198, 66, 21);
        frame.getContentPane().add(textField_7);
        textField_7.setColumns(10);
        
        label_1 = new JLabel("\u6B63\u786E\u7B54\u6848");
        label_1.setFont(new Font("宋體", Font.PLAIN, 14));
        label_1.setBounds(10, 200, 66, 15);
        frame.getContentPane().add(label_1);
        
        label_2 = new JLabel("\u8BF7\u89E3\u7B54");
        label_2.setFont(new Font("宋體", Font.PLAIN, 14));
        label_2.setBounds(10, 152, 58, 18);
        frame.getContentPane().add(label_2);
        
        label_3 = new JLabel("\u6B63\u786E\u6570\u76EE");
        label_3.setFont(new Font("宋體", Font.PLAIN, 14));
        label_3.setBounds(12, 245, 66, 15);
        frame.getContentPane().add(label_3);
        
        textField_8 = new JTextField();
        textField_8.setEditable(false);
        textField_8.setBounds(96, 243, 66, 21);
        frame.getContentPane().add(textField_8);
        textField_8.setColumns(10);
        
        label_4 = new JLabel("\u9519\u8BEF\u6570\u76EE");
        label_4.setBounds(189, 246, 54, 15);
        frame.getContentPane().add(label_4);
        
        textField_10 = new JTextField();
        textField_10.setEditable(false);
        textField_10.setBounds(257, 243, 66, 21);
        frame.getContentPane().add(textField_10);
        textField_10.setColumns(10);
        
        JLabel label_5 = new JLabel("\u6B63\u786E\u7387");
        label_5.setBounds(334, 246, 54, 15);
        frame.getContentPane().add(label_5);
        
        textField_11 = new JTextField();
        textField_11.setEditable(false);
        textField_11.setBounds(379, 243, 66, 21);
        frame.getContentPane().add(textField_11);
        textField_11.setColumns(10);
        
        btnNewButton = new JButton("\u7B80\u5355");
        btnNewButton.setBounds(81, 85, 108, 23);
        frame.getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new T_Event());
        
        btnNewButton_1 = new JButton("\u9002\u4E2D");
        btnNewButton_1.setBounds(209, 85, 109, 23);
        frame.getContentPane().add(btnNewButton_1);
        btnNewButton_1.addActionListener(new T_Event());
        
        btnNewButton_2 = new JButton("\u56F0\u96BE");
        btnNewButton_2.setBounds(336, 85, 109, 23);
        frame.getContentPane().add(btnNewButton_2);
        btnNewButton_2.addActionListener(new T_Event());
        
        button = new JButton("\u9000\u51FA");
        button.setBounds(96, 390, 93, 23);
        frame.getContentPane().add(button);
        button.addActionListener(new T_Event());
        
        button_1 = new JButton("\u52A0\u6CD5");
        button_1.setBounds(64, 39, 93, 23);
        frame.getContentPane().add(button_1);
        button_1.addActionListener(new T_Event());
        
        button_2 = new JButton("\u51CF\u6CD5");
        button_2.setBounds(164, 39, 93, 23);
        frame.getContentPane().add(button_2);
        button_2.addActionListener(new T_Event());
        
        button_3 = new JButton("\u4E58\u6CD5");
        button_3.setBounds(267, 39, 93, 23);
        frame.getContentPane().add(button_3);
        button_3.addActionListener(new T_Event());
        
        
        btnNewButton_3 = new JButton("\u9664\u6CD5");
        btnNewButton_3.setBounds(369, 39, 93, 23);
        frame.getContentPane().add(btnNewButton_3);
        
        textField_9 = new JTextField();
        textField_9.setEditable(false);
        textField_9.setBounds(84, 340, 368, 21);
        frame.getContentPane().add(textField_9);
        textField_9.setColumns(10);
        
        JLabel label_6 = new JLabel("\u53CD\u9988\u6846");
        label_6.setBounds(10, 346, 54, 15);
        frame.getContentPane().add(label_6);
        
        label_7 = new JLabel("\u83DC\u5355\u680F");
        label_7.setFont(new Font("宋體", Font.PLAIN, 14));
        label_7.setBounds(10, 43, 44, 15);
        frame.getContentPane().add(label_7);
        
        lblNewLabel_1 = new JLabel("\u96BE\u4EE5\u7A0B\u5EA6");
        lblNewLabel_1.setFont(new Font("宋體", Font.PLAIN, 14));
        lblNewLabel_1.setBounds(10, 89, 64, 15);
        frame.getContentPane().add(lblNewLabel_1);
        
        comboBox = new JComboBox(Skin);
        comboBox.setBounds(96, 292, 66, 21);
        frame.getContentPane().add(comboBox);
        comboBox.addActionListener(new T_Event());
        
        JLabel lblNewLabel_2 = new JLabel("\u8BBE\u7F6E");
        lblNewLabel_2.setFont(new Font("宋體", Font.PLAIN, 14));
        lblNewLabel_2.setBounds(12, 294, 44, 15);
        frame.getContentPane().add(lblNewLabel_2);
        
        textField_12 = new JTextField();
        textField_12.setBounds(257, 292, 66, 21);
        frame.getContentPane().add(textField_12);
        textField_12.setColumns(10);
        
        
        JLabel label_8 = new JLabel("\u9898\u91CF");
        label_8.setBounds(189, 295, 54, 15);
        frame.getContentPane().add(label_8);
        
         btnNewButton_4 = new JButton("\u518D\u6765\u4E00\u6B21");//再來一次
        
        btnNewButton_4.setBounds(295, 390, 93, 23);
        frame.getContentPane().add(btnNewButton_4);
        btnNewButton_4.addActionListener(new T_Event());
        
        //frame.setVisible(true);//184696141
        btnNewButton_3.addActionListener(new T_Event());
        
        l=new Login();
        l.Set_frame(true);
        
        
        
        
    }
}

 

 

演示過程:

收穫與感悟:

        (  在這一次結對子小專案中  )

  • 我主要充當領航員的作用,我的同伴充當著駕駛員的作用。
  • 我的同伴主要是對程式進行編碼,然後由我來後期除錯。
  • 一開始,我們兩個分別獨立完成一部分,後來發現,彼此的思想很難溝通且達成共識,於是,我們決定在課堂上好好珍惜面對面交流的時間,在課下利用QQ、微信、電話和遠端操控的方式來進行深入探討。
  • 翅膀只剩一邊是飛翔不起來的,儘管很努力的飛到半空,也不夠雙翅滑翔的輕鬆自如,所以合作真的很重要。在合作中,只有互相交流、交換思想,才會發現自己的優勢和不足之處在哪裡。因為有了時間的限制,所以有了無形的壓力,所以我們更深的領悟到了時間觀念的重要性。
  • 我們在完成這次專案的時候,我們彼此改正缺點,彼此進步,得到的一點一滴的努力和進步,都會頗感欣慰。
  • 由於有了同伴的監督,讓我不敢偷懶,讓我更認真的去檢視和更正程式的不足,有了一個方向和目標,讓我們更有追求。
  • 當完成程式的所有測試後,很有成就感。
  • 非常期待下一次的結對專案。

結對工作過程:

 

---恢復內容結束---

相關文章