首页 > 技术文章 > 四则运算

qinlai 2021-09-27 20:07 原文

一、题目要求

可以自动生成四则运算题目,能判断结果。

二、PSP表格

    预计耗时(分钟) 实际耗时(分钟)
Planning 计划 20 20
Estimate 估计这个任务需要多少时间 5 5
Development 开发 80 90
Analysis 需求分析 10 10
Design Spec 生成设计文档 / /
Design Review 设计复审(和同事审核设计文档) / /
Coding Standerd 代码规范(为目前的开发制定合适的规范) / /
Design 具体设计 30 30
Coding 具体编码 100 100
Code Review 代码复审 10 15
Text 测试(自测,修改代码,提交修改) 20 20
Reporting 报告 20 20
Text Report 测试报告 10 10
Size Measurement 计算工作量 5 5
Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 5 5
Sum 合计 305 330

 

三、代码

1、MainMenu.java主菜单

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

public class MainMenu extends JFrame implements ActionListener{

    private static final long serialVersionUID = 1L;
    static int max = 20;// 声明int类型的全局变量max,为了给其他类的调用
    static int operat = 2;// 声明运算符:1为+,2为-,3为*,4为/
            
    // 主窗口中的组件
    Container winContainer;// 声明容器
    JMenuBar mbar;// 声明菜单栏
    JMenu fileMenu,systemMenu;// 声明菜单组
    JMenuItem startMenuItem,setMenuItem;// 声明子菜单项
    JMenuItem exitMenuItem,helpMenuItem;
    
    JLabel jl_hello;// 声明label
    
    public MainMenu() {
        super("四则运算");
        this.setBounds(400,150,500,400);// (x,y,width,height)
        winContainer = this.getContentPane();// 获取当前框架的容器
        winContainer.setLayout(null);// 自定义编辑布局方法(取消容器布局方式)
        
        jl_hello = new JLabel("口算GO GO GO!");
        jl_hello.setBounds(140, 100, 200, 40);
    
        jl_hello.setForeground(new Color(255,130,71));
        jl_hello.setFont(new Font("微软雅黑", Font.BOLD, 30));
        
        // 实例化菜单栏对象
        mbar = new JMenuBar();
        
        // 实例化菜单组
        fileMenu = new JMenu("开始游戏");
        systemMenu = new JMenu("退出");

        // 实例化子菜单项
        startMenuItem = new JMenuItem("开始游戏");
        exitMenuItem = new JMenuItem("退出");
        
        
        // 注册监听器
        startMenuItem.addActionListener(this); 
        exitMenuItem.addActionListener(this);
        
        
        // 将开始、参数设置子菜单项添加到文件菜单组中
        this.setJMenuBar(mbar);
        mbar.add(fileMenu);
        mbar.add(systemMenu);
        fileMenu.add(startMenuItem);
        systemMenu.add(exitMenuItem);
    
    
        // 将退出、帮助子菜单项添加到系统菜单组中
        
    
        // 将欢迎标签添加到Container容器中
        winContainer.add(jl_hello);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 框架关闭时,退出JVM
    }
    
    public void actionPerformed(ActionEvent e) {
        
        if(e.getSource() == startMenuItem) {// 开始游戏按钮触发事件
            winContainer.removeAll();// 清空当前winContainer(用户定义的Container名)上的所有控件
            Game g1 = new Game();
            g1.setSize(500, 400);
            g1.setLayout(null);// 设置为null即为清空布局管理器,之后添加组件
            winContainer.add(g1);
            winContainer.repaint();
            //new Game().setVisible(true);// 实例化游戏界面并设置可见性为true
            
        }else if(e.getSource() == exitMenuItem) {// 退出按钮触发事件
            System.exit(0);// 退出JVM
            
        }
    }
    
    public static void main(String[] args) {// 主方法
        new MainMenu().setVisible(true);
    }
 
}
 

2、Game.java随机生成四则运算

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

public class Game extends JPanel implements ActionListener{
 
    private static final long serialVersionUID = 1L;
    
    JLabel label[];
    JLabel err[];
    JTextField text[];
    static String[] question;
    static int[] result;
 
    JLabel head;// 声明标题标签
    JButton jb_enter;// 声明确认按钮
    JButton jb_reset;// 声明重置按钮
    
    public Game(){// 构造方法
        //super("四则运算");// 初始化父类型特征
        
        label = new JLabel[10];// 显示10个随机的式子
        err = new JLabel[10];// 显示10个提示信息
        text = new JTextField[10];// 10个文本输入
        question = new String[10];// 接收随机式子的字符串
        result = new int[10];// 接收结果int变量
        
        Tools.rand();// 调用工具类中生成十个式子的方法
        head = new JLabel("口算Go Go GO");
        head.setBounds(145, -10, 120, 100);// 设置标签的显示位置和大小 
        head.setForeground(new Color(131,188,255));// 设置标签字体颜色
        head.setFont(new Font("微软雅黑", Font.BOLD, 15));// 字体,加粗,字号
        
        jb_enter = new JButton("确认");// 创建“确认”按钮对象
        jb_reset = new JButton("重置");// 创建“重置”按钮对象
        jb_enter.setBounds(110, 190, 60, 20);
        jb_reset.setBounds(245, 190, 60, 20);
        
        // 注册监听器
        jb_enter.addActionListener(this);
        jb_reset.addActionListener(this);
        
        // 将组件添加到框架中
        this.add(jb_reset);
        this.add(jb_enter);
        this.add(head);
        
        // 循环出十个式子,每行两道题
        for(int i = 0; i<10; i+=2){
            label[i] = new JLabel(question[i]);// 第一列第i个式子
            label[i].setBounds(70,70+10*i,60,15);
            
            err[i]= new JLabel("");// 定义第一列第i个内容为""提示标签
            err[i].setBounds(180,70+10*i,40,15);
            err[i].setForeground(new Color(239,131,84));
 
            text[i] = new JTextField("",20);// 第一列第i行文本输入框
            text[i].setBounds(130,70+10*i,40,15);
 
            label[i+1] = new JLabel(question[i+1]);// 第二列第i个式子
            label[i+1].setBounds(230,70+10*i,60,15);
 
            err[i+1] = new JLabel("");// 定义第二列第i个内容为""提示标签
            err[i+1].setBounds(340,70+10*i,40,15);
            err[i+1].setForeground(new Color(239,131,84));
 
            text[i+1] = new JTextField("",20);// 第二列第i行文本输入框
            text[i+1].setBounds(290,70+10*i,40,15);
            
            // 将组件添加到框架中
            this.add(label[i]);
            this.add(label[i+1]);
            this.add(err[i]);
            this.add(err[i+1]);
            this.add(text[i]);
            this.add(text[i+1]);
        }
        
        this.setVisible(true);//可见性为true
    }
 
    int error = 0;// 错误题数计数器
    // 监听按钮事件
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()== jb_enter){// 单击确认按钮
            for(int i = 0; i<10;i++){
                try{
                    if(result[i] != Integer.parseInt(text[i].getText())){
                        error++;
                        err[i].setText("错误!");// 更改第i个err[]文本为错误
                    }else{
                        err[i].setText("正确!");// 更改第i个err[]文本为正确
                    }
                }catch(NumberFormatException f){
                    JOptionPane.showMessageDialog(this,"请全部输入!","警告",JOptionPane.WARNING_MESSAGE);
                    error++;
                    break;
                }
            }
            if(error == 0){
                JOptionPane.showMessageDialog(this,"过关!","恭喜",1 );
            }
        }else if(e.getSource()==jb_reset){
            Tools.rand();// 调用生成10个式子的方法
            for(int i=0;i<10;i++){//通过循环重新赋值
                err[i].setText("");
                text[i].setText("");
                label[i].setText(question[i]);
            }
        }
    }
 
}

3、Tools.java

package Arithmetic;
import java.util.Random;

public class Tools {
    /**
     *    生成十个式子的方法
     */
    public static void rand(){
        int a,b,operator;
        Random ran = new Random();
        for(int i= 0;i<10;i++){
            a= ran.nextInt(MainMenu.max)+1;// 随机数范围
            b= ran.nextInt(MainMenu.max)+1;
            operator=ran.nextInt(MainMenu.operat)+1;// 自定义运算符
            switch(operator){
                case 1:// "+"
                    Game.question[i] = a + "+" + b + "=";
                    Game.result[i] = a + b;
                    break;
                case 2:// "-"
                    Game.question[i] = a + "-" + b + "=";
                    Game.result[i] = a - b;
                    break;
                case 3:// "*"
                    Game.question[i] = a + "*" + b + "=";
                    Game.result[i] = a * b;
                    break;
                case 4:// "/"
                    Game.question[i] = a + "/" + b + "=";
                    Game.result[i] = a / b;
                    break;
            }
        }
    }
 
}

 

四、效果展示

 

 

 

推荐阅读