Java 制作四则运算题库

前几天,母上找我给我小妹(一年级生)做个四则运算题库程序。
我顶着被我妹骂的压力,紧赶慢赶做了出来。
用到了这几天学的Swing组件。
比较繁琐,以后可能会改进。

运算类

import com.sun.xml.internal.bind.WhiteSpaceProcessor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Operation extends JFrame implements ActionListener {
    //定义加减乘除的数字
    int Add_a,Add_b,Add_c;
    int Sub_a,Sub_b,Sub_c;
    int Mul_a,Mul_b,Mul_c;
    int Div_a,Div_b = 1,Div_c,Div_mod;


    //上一题下一题的按钮
    private JButton NextTest;
    private JButton EndStudy;

    //定义一个提交按钮
    private JButton Submit;

    //定义运算式的文本
    private JLabel Explain;
    private JLabel Explain_MOD;

    //定义一个文本框用来判断输入的数字是否正确
    private JTextField
            StudyTestADD = new JTextField(20),
            StudyTestSUB = new JTextField(20),
            StudyTestMUL = new JTextField(20),
            StudyTestDIV = new JTextField(20);
    private JTextField
            Answer = new JTextField(3),
            Answer_MOD = new JTextField(3);
    private JTextField Result = new JTextField(50);
    //随机数
    Random random = new Random();
    //定义的这个是为了随机加减乘除
    int Operation_random = random.nextInt(4);

    public void CreateTestWindow(){
        Explain = new JLabel("你的答案是:");
        this.add(Explain);
        Explain.setBounds(150,100,150,50);
        Explain.setFont(new Font("宋体", Font.BOLD,20));

        Explain_MOD = new JLabel("如果是除法,那么它的余数是:");
        this.add(Explain_MOD);
        Explain_MOD.setBounds(50,160,450,50);
        Explain_MOD.setFont(new Font("宋体", Font.BOLD,20));

        this.add(Answer);
        Answer.setBounds(300,100,50,50);
        Answer.setFont(new Font("宋体", Font.BOLD,20));

        this.add(Answer_MOD);
        Answer_MOD.setBounds(460,160,50,50);
        Answer_MOD.setFont(new Font("宋体", Font.BOLD,20));

        Submit = new JButton("提交");
        this.add(Submit);
        Submit.setBounds(360,50,100,50);
        Submit.setFont(new Font("宋体", Font.BOLD,20));

        this.add(Result);
        Result.setBounds(40,300,250,100);
        //设置文本框不能输入
        Result.setEditable(false);
        Result.setBackground(Color.WHITE);
    }

    public Operation() {
        super("题目");
        this.setSize(600, 500);
        this.setLocation(600, 300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //定义按钮格式
        setLayout(null);

        NextTest = new JButton("下一题");
        this.add(NextTest);
        NextTest.setBounds(380, 250, 160, 50);
        NextTest.setFont(new Font("宋体", Font.BOLD,20));
        NextTest.addActionListener(this);

        EndStudy = new JButton("结束做题");
        this.add(EndStudy);
        EndStudy.setBounds(380, 350, 160, 50);
        EndStudy.setFont(new Font("宋体", Font.BOLD,20));
        EndStudy.addActionListener(this);

        switch (Operation_random){
            //定义加法
            case 0:
                Add_a = random.nextInt(51);
                Add_b = random.nextInt(51);

                StudyTestADD.setText(Add_a+"+"+Add_b+"=");
                this.add(StudyTestADD);
                StudyTestADD.setBounds(150,50,200,50);
                StudyTestADD.setFont(new Font("宋体", Font.BOLD,20));
                StudyTestADD.setEditable(false);

                CreateTestWindow();

                Submit.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Add_c = Integer.parseInt(Answer.getText());
                        if(Add_c == Add_a + Add_b) {
                            Result.setText("正确");
                            Result.setFont(new Font("宋体", Font.BOLD,20));
                        }
                        else {
                            Result.setText("错误,正确答案是" + (Add_a + Add_b));
                            Result.setFont(new Font("宋体", Font.BOLD,20));
                        }
                    }
                });

            //定义减法
            case 1:
                Sub_a = random.nextInt(101)+50;
                Sub_b = random.nextInt(51);

                StudyTestSUB.setText(Sub_a+"-"+Sub_b+"=");
                this.add(StudyTestSUB);
                StudyTestSUB.setBounds(150,50,200,50);
                StudyTestSUB.setFont(new Font("宋体", Font.BOLD,20));
                StudyTestSUB.setEditable(false);

                CreateTestWindow();

                Submit.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Sub_c = Integer.parseInt(Answer.getText());
                        if(Sub_c == Sub_a - Sub_b) {
                            Result.setText("正确");
                            Result.setFont(new Font("宋体", Font.BOLD,20));
                        }
                        else {
                            Result.setText("错误,正确答案是" + (Sub_a - Sub_b));
                            Result.setFont(new Font("宋体", Font.BOLD,20));
                        }
                    }
                });

            //定义乘法
            case 2:
                Mul_a = random.nextInt(11);
                Mul_b = random.nextInt(11);

                StudyTestMUL.setText(Mul_a+"×"+Mul_b+"=");
                this.add(StudyTestMUL);
                StudyTestMUL.setBounds(150,50,200,50);
                StudyTestMUL.setFont(new Font("宋体", Font.BOLD,20));
                StudyTestMUL.setEditable(false);

                CreateTestWindow();

                Submit.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Mul_c = Integer.parseInt(Answer.getText());
                        if(Mul_c == Mul_a * Mul_b) {
                            Result.setText("正确");
                            Result.setFont(new Font("宋体", Font.BOLD,20));
                        }
                        else {
                            Result.setText("错误,正确答案是" + (Mul_a * Mul_b));
                            Result.setFont(new Font("宋体", Font.BOLD,20));
                        }
                    }
                });

            //定义除法
            case 3:
                Div_a = random.nextInt(101)+1;
                Div_b = random.nextInt(11)+1;

                StudyTestDIV.setText(Div_a+"÷"+Div_b+"=");
                this.add(StudyTestDIV);
                StudyTestDIV.setBounds(150,50,200,50);
                StudyTestDIV.setFont(new Font("宋体", Font.BOLD,20));
                StudyTestDIV.setEditable(false);

                CreateTestWindow();

                Submit.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Div_c = Integer.parseInt(Answer.getText());
                        Div_mod = Integer.parseInt(Answer_MOD.getText());
                        if(Div_c == Div_a / Div_b && Div_mod == Div_a % Div_b) {
                            Result.setText("正确");
                            Result.setFont(new Font("宋体", Font.BOLD,20));
                        }
                        else {
                            Result.setText("错误,正确答案是" + (Div_a / Div_b) + ",余数为:" + (Div_a % Div_b));
                            Result.setFont(new Font("宋体", Font.BOLD,20));
                        }
                    }
                });

        }

        this.setVisible(true);
    }


    /**
     * Invoked when an action occurs.
     *
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == NextTest){
            this.dispose();;
            Operation studyWindow = new Operation();
        }
        if(e.getSource() == EndStudy){
            System.exit(0);
        }
    }
}

菜单类

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

public class Menu extends JFrame implements ActionListener {
    private JButton Study_Start;
    private JButton Study_Over;
    private JLabel Study_Title;

    public Menu(){
        super("四则运算");
        this.setSize(600,500);
        this.setLocation(600,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //定义文字
        Study_Title = new JLabel("四则运算小测试");
        this.add(Study_Title);
        Study_Title.setBounds(200,50,200,100);
        Study_Title.setFont(new Font("宋体",Font.BOLD,25));

        //定义按钮
        setLayout(null);
        Study_Start = new JButton("开始做题");
        Study_Over = new JButton("结束做题");
        this.add(Study_Start);
        this.add(Study_Over);
        Study_Start.setBounds(40,250,160,50);
        Study_Over.setBounds(380,250,160,50);
        Study_Start.addActionListener(this);
        Study_Over.addActionListener(this);
        Study_Start.setFont(new Font("宋体", Font.BOLD,20));
        Study_Over.setFont(new Font("宋体", Font.BOLD,20));

        this.setVisible(true);
    }

    public static void main(String[] args) {
        Menu menu = new Menu();
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == Study_Start){
            this.dispose();
            new Operation();
        }
        if(e.getSource() == Study_Over){
            System.exit(0);
        }
    }
}

主要是按钮的定义和布局比较繁琐。
而且最后做的界面还是有点不好看的233.
界面
题库内容

随机了四种类型,加减乘除。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值