小学生四则运算

PSP:

psp 任务内容 计划完成需要的时间(min) 实际完成需要的时间(min)
Planning 计划 30 40
Estimate 估计这个任务需要多少时间,并规划大致工作步骤 30 20
Development 开发 120 150
Analysis 需求分析(包括学习新技术) 45 20
Design Spec 生成设计文档 20 0
Coding Standard 代码规范 10 10
Design 具体设计 15 22
Coding 具体编码 40 35
Code Review 代码复审 10 1
Test 测试(自我测试,修改代码,提交修改) 20 20
Reporting 报告 10 10
Test Report 测试报告 15 10

代码:

package text;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Chuti{
    public static void main (String args[]) {
        new WinJFrame();    
    } 
}

class WinJFrame extends JFrame{
    public WinJFrame(){
        setBounds(400,200,600,400);
        setTitle("小学生四则运算练习器");
        setLayout(null);
        Container con = getContentPane();
        con.setBackground(new Color(255,218,185));
        
        init();
        
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public int getPlus(String s) {
        String regex = "[^0123456789.]+";
        s=s.replaceAll(regex, "#");
        StringTokenizer fenxi = new StringTokenizer(s,"#");
        int sum = 0;
        while(fenxi.hasMoreTokens()) {
            String item = fenxi.nextToken();
            int h =Integer.parseInt(item);
            sum = sum + h;
        }
        return sum;
    }
    
    public int getMinus(String s) {
        String regex = "[^0123456789.]+";
        s=s.replaceAll(regex, "#");
        StringTokenizer fenxi = new StringTokenizer(s,"#");
        int sum = 0;
        int ss[];
        ss=new int[2];
        for(int i=0;fenxi.hasMoreTokens();i++)
        {
            String item = fenxi.nextToken();
            ss[i]=Integer.parseInt(item);
        }
        sum=ss[0]-ss[1];
    
        return sum;
    }
    
    public int getMultiply(String s) {
        String regex = "[^0123456789.]+";
        s=s.replaceAll(regex, "#");
        StringTokenizer fenxi = new StringTokenizer(s,"#");
        int sum = 0;
        int ss[];
        ss=new int[2];
        for(int i=0;fenxi.hasMoreTokens();i++)
        {
            String item = fenxi.nextToken();
            ss[i]=Integer.parseInt(item);
        }
        sum=ss[0]*ss[1];
    
        return sum;
    }
    
    public int getDivision(String s) {
        String regex = "[^0123456789.]+";
        s=s.replaceAll(regex, "#");
        StringTokenizer fenxi = new StringTokenizer(s,"#");
        int sum = 0;
        int ss[];
        ss=new int[2];
        for(int i=0;fenxi.hasMoreTokens();i++)
        {
            String item = fenxi.nextToken();
            ss[i]=Integer.parseInt(item);
        }
        sum=ss[0]/ss[1];
    
        return sum;
    }
    
    void init() {
        JButton b[] = new JButton[12];
        for(int k=0;k<12;k++) {
            b[k] = new JButton();
            add(b[k]);
        }
        
        b[0].setBounds(20,20,100,30);
        b[0].setText("加法");
        b[1].setBounds(460,20,100,30);
        b[1].setText("换题");
        b[2].setBounds(20,60,100,30);
        b[2].setText("确认答案");
        
        b[3].setBounds(20,100,100,30);
        b[3].setText("减法");
        b[4].setBounds(460,100,100,30);
        b[4].setText("换题");
        b[5].setBounds(20,140,100,30);
        b[5].setText("确认答案");
        
        b[6].setBounds(20,180,100,30);
        b[6].setText("乘法");
        b[7].setBounds(460,180,100,30);
        b[7].setText("换题");
        b[8].setBounds(20,220,100,30);
        b[8].setText("确认答案");
        
        b[9].setBounds(20,260,100,30);
        b[9].setText("除法");
        b[10].setBounds(460,260,100,30);
        b[10].setText("换题");
        b[11].setBounds(20,300,100,30);
        b[11].setText("确认答案");
        
        JTextArea ja0 = new JTextArea();
        JTextArea ja1 = new JTextArea();
        JTextArea ja2 = new JTextArea();
        JTextArea ja3 = new JTextArea();
        
        ja0.setBounds(120, 20, 340, 30);
        ja0.setFont(new Font("黑体",Font.BOLD,30));
        add(ja0);
        
        ja1.setBounds(120, 100, 340, 30);
        ja1.setFont(new Font("黑体",Font.BOLD,30));
        add(ja1);
        
        ja2.setBounds(120, 180, 340, 30);
        ja2.setFont(new Font("黑体",Font.BOLD,30));
        add(ja2);
        
        ja3.setBounds(120, 260, 340, 30);
        ja3.setFont(new Font("黑体",Font.BOLD,30));
        add(ja3);
        
        JTextField jt0 = new JTextField();
        JTextField jt1 = new JTextField();
        JTextField jt2 = new JTextField();
        JTextField jt3 = new JTextField();
        
        jt0.setBounds(120, 60, 340, 30);
        jt0.setFont(new Font("黑体",Font.BOLD,30));
        add(jt0);
        
        jt1.setBounds(120, 140, 340, 30);
        jt1.setFont(new Font("黑体",Font.BOLD,30));
        add(jt1);
        
        jt2.setBounds(120, 220, 340, 30);
        jt2.setFont(new Font("黑体",Font.BOLD,30));
        add(jt2);
        
        jt3.setBounds(120, 300, 340, 30);
        jt3.setFont(new Font("黑体",Font.BOLD,30));
        add(jt3);
        
        for(int j=0;j<2;j++) {
            b[j].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int number1,number2;
                    number1=(int)Math.ceil(Math.random()*100);
                    number2=(int)Math.ceil(Math.random()*100);
                    ja0.setText(number1+"+"+number2+"=");
                }
            });
        }
        
        for(int j=3;j<5;j++) {
            b[j].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int number1,number2;
                    number1=(int)Math.ceil(Math.random()*100);
                    number2=(int)Math.ceil(Math.random()*100);
                    ja1.setText(number1+"-"+number2+"=");
                }
            });
        }
        
        for(int j=6;j<8;j++) {
            b[j].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int number1,number2;
                    number1=(int)Math.ceil(Math.random()*100);
                    number2=(int)Math.ceil(Math.random()*100);
                    ja2.setText(number1+"*"+number2+"=");
                }
            });
        }
        
        for(int j=9;j<11;j++) {
            b[j].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int number1,number2;
                    number1=(int)Math.ceil(Math.random()*100);
                    number2=(int)Math.ceil(Math.random()*100);
                    ja3.setText(number1+"/"+number2+"=");
                }
            });
        }
        
        b[2].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(getPlus(ja0.getText())==Integer.parseInt(jt0.getText())){
                    JOptionPane.showMessageDialog(jt0,"答案正确","判定结果",JOptionPane.INFORMATION_MESSAGE);    
                }
                else{
                    JOptionPane.showMessageDialog(jt0,"答案错误","判定结果",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        
        b[5].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(getMinus(ja1.getText())==Integer.parseInt(jt1.getText())){
                    JOptionPane.showMessageDialog(jt1,"答案正确","判定结果",JOptionPane.INFORMATION_MESSAGE);    
                }
                else{
                    JOptionPane.showMessageDialog(jt1,"答案错误","判定结果",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        
        b[8].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(getMultiply(ja2.getText())==Integer.parseInt(jt2.getText())){
                    JOptionPane.showMessageDialog(jt2,"答案正确","判定结果",JOptionPane.INFORMATION_MESSAGE);    
                }
                else{
                    JOptionPane.showMessageDialog(jt2,"答案错误","判定结果",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        
        b[11].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(getDivision(ja3.getText())==Integer.parseInt(jt3.getText())){
                    JOptionPane.showMessageDialog(jt3,"答案正确","判定结果",JOptionPane.INFORMATION_MESSAGE);    
                }
                else{
                    JOptionPane.showMessageDialog(jt3,"答案错误","判定结果",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        }
    }

 

测试:

小学生四则运算

小学生四则运算

 

 

 

上一篇:Flutter实现mm:ss(分秒)倒计时组件


下一篇:华为机试——坐标移动