天天看點

四則運算實作2(JAVA)

之前做了個流程版的,這裡放最後的總結版

先講功能實作:

1.整體程式是個100以内的四則運算出題器;

2.做了個選擇菜單,你可以自由選擇需要做加減乘除任意一項的四則運算練習;

3.增加了内層中的傳回功能,使得運作過程中不必退出;

4.出題器中提供出題,檢驗答案和揭示答案功能。

下面是程式實作截圖以及代碼實作:

菜單面闆:

四則運算實作2(JAVA)
四則運算實作2(JAVA)
四則運算實作2(JAVA)

package Live;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class SizeCaidan extends JFrame{
    
    JFrame f = new JFrame("運算選擇");
    JPanel p = new JPanel();
    
     public SizeCaidan() {
        JButton b1 = new JButton("加法練習");
        JButton b2 = new JButton("減法練習");
        JButton b3 = new JButton("乘法練習");
        JButton b4 = new JButton("除法練習");
        JButton ex = new JButton("退出");
        b1.addActionListener(new Monitor2());
        b2.addActionListener(new Monitor2());
        b3.addActionListener(new Monitor2());
        b4.addActionListener(new Monitor2());
        ex.addActionListener(new Monitor2());
        f.addWindowListener(new Wind());
        f.add(p);
        p.setLayout(new GridLayout(1, 2));
        
        Icon icon=new ImageIcon("src/Live/gdjssfdx.jpg");//
        JLabel label=new JLabel(icon);
        label.setBounds(0,0,400,350);
        f.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
        
        JPanel jp = (JPanel)f.getContentPane();
        jp.setOpaque(false);//
        
        
        JLabel l = new JLabel();
        JPanel p2 = new JPanel();
        p.add(l);
        p.add(p2);
        p2.setLayout(new GridLayout(5, 1));
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(b4);
        p2.add(ex);
        
        p.setOpaque(false);
        
        f.setBounds(200, 200, 400, 400);
        f.setVisible(true);
    }
    
    
    

    public static void main(String[] args) {
        new SizeCaidan();
    }

    private class Wind extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            f.setVisible(false);
            System.exit(0);
        }
    }

    private class Monitor2 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("退出")) {
                //new SizeCaidan();
                System.exit(0);
            }

            if (e.getActionCommand().equals("加法練習")) {
                new NumJframe();
                f.setVisible(false);
            }
            
            if (e.getActionCommand().equals("減法練習")) {
                new NumJframe2();
                f.setVisible(false);
            }
            if (e.getActionCommand().equals("乘法練習")) {
                new NumJframe3();
                f.setVisible(false);
            }
            if (e.getActionCommand().equals("除法練習")) {
                new NumJframe4();
                f.setVisible(false);
            }
            
        }
    }

}      

菜單面闆

加法練習面闆:

四則運算實作2(JAVA)
四則運算實作2(JAVA)
四則運算實作2(JAVA)
四則運算實作2(JAVA)
四則運算實作2(JAVA)
package Live;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import Live.JiafaSum;

public class Jia_1 {
    public static void main(String[] args) {
        new NumJframe();
    }
}

class JiafaSum {// 定義全局變量(其實就是類變量,因為在JAVA中不存在全局變量的聲明,但是類預設是static的,
                // 是以采用定義類變量的方法)
    public static int sum = 0;
}

class NumJframe extends JFrame {
    public NumJframe() {
        setLayout(null);
        Container con = getContentPane();// 建立一個内容面闆容器
        bank1();// 獲得題目
        bank2();// 确認答案
        bank3();//傳回
        bank4();//揭示答案
        setTitle("100以內的加法測試");
        setBounds(200, 200, 600, 300);// 設定出現位置和視窗大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設定關閉方式
        setVisible(true);// 設定是否展示視窗
        con.setBackground(Color.yellow);
    }
    
    void bank1() {// 獲得題目按鈕區
        JTextField jt = new JTextField();// 文本框1
        jt.setBounds(120, 40, 200, 30);
        JButton jb1 = new JButton();// 按鈕1
        jb1.setBounds(20, 40, 100, 30);
        jb1.setText("獲得題目");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int jiashu1 = (int) (Math.random() * 100 + 1);
                int jiashu2 = (int) (Math.random() * 100 + 1);
                String js1 = "" + jiashu1;
                String js2 = "" + jiashu2;
                JiafaSum.sum = jiashu1 + jiashu2;
                jt.setText(js1 + "+" + js2 + "=" + "");
            }
        });
        add(jt);
        add(jb1);
    }
    
    void bank2() {// 确認答案按鈕區
        JTextField jt2 = new JTextField();// 文本框2
        jt2.setBounds(120, 75, 200, 30);
        JButton jb2 = new JButton();// 按鈕2
        jb2.setBounds(20, 75, 100, 30);
        jb2.setText("确認答案");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int sum = JiafaSum.sum;
                String inputSum = jt2.getText();
                
                //System.out.println("sum="+sum);
                //System.out.println("inputSum="+inputSum);
                if(inputSum == null) {
                    System.out.println("你的輸入有誤,請重新輸入一個整數:");
                }
                int inputsum = Integer.parseInt(inputSum);
                if (inputsum == sum) {
                    JOptionPane.showMessageDialog(null,"答案正确","消息對話框",JOptionPane.INFORMATION_MESSAGE);
                }

                else {
                    JOptionPane.showMessageDialog(null,"答案錯誤","消息對話框",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        add(jt2);
        add(jb2);
    }
    
    void bank3() {//傳回區
        JButton jb3 = new JButton();//
        jb3.setBounds(0, 0, 100, 25);
        jb3.setText("傳回");
        jb3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new SizeCaidan();
            }
        });
    add(jb3);
    }
    
    void bank4() {//輸出答案區
        JTextField jt4 = new JTextField();// 文本框1
        jt4.setBounds(120, 110, 200, 30);
        JButton jb4 = new JButton();// 按鈕1
        jb4.setBounds(20, 110, 100, 30);
        jb4.setText("揭示答案");
        jb4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jt4.setText(JiafaSum.sum+"");
            }
        });
        add(jt4);
        add(jb4);
        
    }
}      

加法

以加法為例,其他三則運算也類似,以下直接貼出代碼

(PS:除法中因為變量提取為int型,是以除法結果也隻保留整數部分)

四則運算實作2(JAVA)
四則運算實作2(JAVA)
package Live;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Jian_1 {
    public static void main(String[] args) {
        new NumJframe2();
    }
}

class JianfaSum {// 定義全局變量(其實就是類變量,因為在JAVA中不存在全局變量的聲明,但是類預設是static的,
                    // 是以采用定義類變量的方法)
    public static int sum2 = 0;
}

class NumJframe2 extends JFrame {
    public NumJframe2() {
        setLayout(null);
        Container con = getContentPane();// 建立一個内容面闆容器
        bank1();// 獲得題目
        bank2();// 确認答案
        bank3();
        bank4();
        setTitle("100以內的減法測試");
        setBounds(200, 200, 600, 300);// 設定出現位置和視窗大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設定關閉方式
        setVisible(true);// 設定是否展示視窗
        con.setBackground(Color.cyan);
    }

    void bank1() {// 獲得題目按鈕區
        JTextField jt = new JTextField();// 文本框1
        jt.setBounds(120, 40, 200, 30);
        JButton jb1 = new JButton();// 按鈕1
        jb1.setBounds(20, 40, 100, 30);
        jb1.setText("獲得題目");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int jianshu1 = (int) (Math.random() * 100 + 1);
                int jianshu2 = (int) (Math.random() * 100 + 1);
                String js1 = "" + jianshu1;
                String js2 = "" + jianshu2;
                JianfaSum.sum2 = jianshu1 - jianshu2;
                jt.setText(js1 + "-" + js2 + "=" + "");
            }
        });
        add(jt);
        add(jb1);
    }

    void bank2() {// 确認答案按鈕區
        JTextField jt2 = new JTextField();// 文本框2
        jt2.setBounds(120, 75, 200, 30);
        JButton jb2 = new JButton();// 按鈕1
        jb2.setBounds(20, 75, 100, 30);
        jb2.setText("确認答案");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int sum = JianfaSum.sum2;
                String inputSum = jt2.getText();

                // System.out.println("sum="+sum);
                // System.out.println("inputSum="+inputSum);
                if (inputSum == null) {
                    System.out.println("你的輸入有誤,請重新輸入一個整數:");
                }
                int inputsum = Integer.parseInt(inputSum);
                if (inputsum == sum) {
                    JOptionPane.showMessageDialog(null, "答案正确", "消息對話框", JOptionPane.INFORMATION_MESSAGE);
                }

                else {
                    JOptionPane.showMessageDialog(null, "答案錯誤", "消息對話框", JOptionPane.WARNING_MESSAGE);
                }
            }

        });
        add(jt2);
        add(jb2);
    }
    void bank3() {//傳回區
           JButton jb3 = new JButton();//
           jb3.setBounds(0, 0, 100, 25);
           jb3.setText("傳回");
           jb3.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   setVisible(false);
                   new SizeCaidan();
               }
           });
       add(jb3);
    }
    void bank4() {//輸出答案區
        JTextField jt4 = new JTextField();// 文本框1
        jt4.setBounds(120, 110, 200, 30);
        JButton jb4 = new JButton();// 按鈕1
        jb4.setBounds(20, 110, 100, 30);
        jb4.setText("揭示答案");
        jb4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jt4.setText(JianfaSum.sum2+"");
            }
        });
        add(jt4);
        add(jb4);
    }
}      

減法

四則運算實作2(JAVA)
四則運算實作2(JAVA)
package Live;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;



public class Cheng {
    public static void main(String[] args) {
        new NumJframe3();
    }
}

class ChengfaSum {// 定義全局變量(其實就是類變量,因為在JAVA中不存在全局變量的聲明,但是類預設是static的,
                // 是以采用定義類變量的方法)
    public static int sum3 = 0;
}

class NumJframe3 extends JFrame {
    public NumJframe3() {
        setLayout(null);
        Container con = getContentPane();// 建立一個内容面闆容器
        bank1();// 獲得題目
        bank2();// 确認答案
        bank3();
        bank4();
        setTitle("100以內的乘法測試");
        setBounds(200, 200, 600, 300);// 設定出現位置和視窗大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設定關閉方式
        setVisible(true);// 設定是否展示視窗
        con.setBackground(Color.darkGray);
    }
    
    void bank1() {// 獲得題目按鈕區
        JTextField jt = new JTextField();// 文本框1
        jt.setBounds(120, 40, 200, 30);
        JButton jb1 = new JButton();// 按鈕1
        jb1.setBounds(20, 40, 100, 30);
        jb1.setText("獲得題目");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int jiashu1 = (int) (Math.random() * 100 + 1);
                int jiashu2 = (int) (Math.random() * 100 + 1);
                String js1 = "" + jiashu1;
                String js2 = "" + jiashu2;
                ChengfaSum.sum3 = jiashu1 * jiashu2;
                jt.setText(js1 + "X" + js2 + "=" + "");
            }
        });
        add(jt);
        add(jb1);
    }
    
    void bank2() {// 确認答案按鈕區
        JTextField jt2 = new JTextField();// 文本框2
        jt2.setBounds(120, 75, 200, 30);
        JButton jb2 = new JButton();// 按鈕1
        jb2.setBounds(20, 75, 100, 30);
        jb2.setText("确認答案");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int sum = ChengfaSum.sum3;
                String inputSum = jt2.getText();
                
                //System.out.println("sum="+sum);
                //System.out.println("inputSum="+inputSum);
                if(inputSum == null) {
                    System.out.println("你的輸入有誤,請重新輸入一個整數:");
                }
                int inputsum = Integer.parseInt(inputSum);
                if (inputsum == sum) {
                    JOptionPane.showMessageDialog(null,"答案正确","消息對話框",JOptionPane.INFORMATION_MESSAGE);
                }

                else {
                    JOptionPane.showMessageDialog(null,"答案錯誤","消息對話框",JOptionPane.WARNING_MESSAGE);
                }
            }



        });
        add(jt2);
        add(jb2);
    }
    void bank3() {//傳回區
          JButton jb3 = new JButton();//
          jb3.setBounds(0, 0, 100, 25);
          jb3.setText("傳回");
          jb3.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  setVisible(false);
                  new SizeCaidan();
              }
          });
      add(jb3);
   }
    void bank4() {//輸出答案區
        JTextField jt4 = new JTextField();// 文本框1
        jt4.setBounds(120, 110, 200, 30);
        JButton jb4 = new JButton();// 按鈕1
        jb4.setBounds(20, 110, 100, 30);
        jb4.setText("揭示答案");
        jb4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jt4.setText(ChengfaSum.sum3+"");
            }
        });
        add(jt4);
        add(jb4);
        
    }
}      

乘法

四則運算實作2(JAVA)
四則運算實作2(JAVA)
package Live;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;



public class Chu_1 {
    public static void main(String[] args) {
        new NumJframe4();
    }
}

class ChufaSum {// 定義全局變量(其實就是類變量,因為在JAVA中不存在全局變量的聲明,但是類預設是static的,
                // 是以采用定義類變量的方法)
    public static int sum4 = 0;
}

class NumJframe4 extends JFrame {
    public NumJframe4() {
        setLayout(null);
        Container con = getContentPane();// 建立一個内容面闆容器
        bank1();// 獲得題目
        bank2();// 确認答案
        bank3();
        bank4();
        setTitle("100以內的除法測試");
        setBounds(200, 200, 600, 300);// 設定出現位置和視窗大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設定關閉方式
        setVisible(true);// 設定是否展示視窗
        con.setBackground(Color.orange);
    }
    
    void bank1() {// 獲得題目按鈕區
        JTextField jt = new JTextField();// 文本框1
        jt.setBounds(120, 40, 200, 30);
        JButton jb1 = new JButton();// 按鈕1
        jb1.setBounds(20, 40, 100, 30);
        jb1.setText("獲得題目");
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int jiashu1 = (int) (Math.random() * 100 + 1);
                int jiashu2 = (int) (Math.random() * 100 + 1);
                String js1 = "" + jiashu1;
                String js2 = "" + jiashu2;
                ChufaSum.sum4 = jiashu1 / jiashu2;
                jt.setText(js1 + "÷" + js2 + "=" + "");
            }
        });
        add(jt);
        add(jb1);
    }
    
    void bank2() {// 确認答案按鈕區
        JTextField jt2 = new JTextField();// 文本框2
        jt2.setBounds(120, 75, 200, 30);
        JButton jb2 = new JButton();// 按鈕1
        jb2.setBounds(20, 75, 100, 30);
        jb2.setText("确認答案");
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int sum = ChufaSum.sum4;
                String inputSum = jt2.getText();
                
                //System.out.println("sum="+sum);
                //System.out.println("inputSum="+inputSum);
                if(inputSum == null) {
                    System.out.println("你的輸入有誤,請重新輸入一個整數:");
                }
                int inputsum = Integer.parseInt(inputSum);
                if (inputsum == sum) {
                    JOptionPane.showMessageDialog(null,"答案正确","消息對話框",JOptionPane.INFORMATION_MESSAGE);
                }

                else {
                    JOptionPane.showMessageDialog(null,"答案錯誤","消息對話框",JOptionPane.WARNING_MESSAGE);
                }
            }



        });
        add(jt2);
        add(jb2);
    }
    void bank3() {//傳回區
          JButton jb3 = new JButton();//
          jb3.setBounds(0, 0, 100, 25);
          jb3.setText("傳回");
          jb3.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  setVisible(false);
                  new SizeCaidan();
              }
          });
      add(jb3);
   }
    
    void bank4() {//輸出答案區
        JTextField jt4 = new JTextField();// 文本框1
        jt4.setBounds(120, 110, 200, 30);
        JButton jb4 = new JButton();// 按鈕1
        jb4.setBounds(20, 110, 100, 30);
        jb4.setText("揭示答案");
        jb4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jt4.setText(ChufaSum.sum4+"");
            }
        });
        add(jt4);
        add(jb4);
        
    }
}      

除法

以上即為該四則運算的最終代碼版。

牢騷時間:

(PS:你看10000遍和她的聊天記錄,她不是你的,你看100遍書,知識就是你的,該醒醒了,我們的目标是建設祖國!)