• 四则运算java


    个人PSP表格

    java代码

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.StringTokenizer;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    
    class WindowActionEvent extends JFrame {
        JTextField text1 = new JTextField(20); // 题目的输出
        JTextField text2 = new JTextField(20); // 答案的输入
        JComboBox choiceFuhao = new JComboBox();
        JButton button1 = new JButton("获取题目");
        JButton button2 = new JButton("确认答案");
        String fuhao;
        
        public WindowActionEvent() {
            setLayout(null);
            choiceFuhao.addItem("选择运算符号:");
            String[] a = { "+", "-", "*", "/" };
            for (int i = 0; i < a.length; i++) {
                choiceFuhao.addItem(a[i]);
            }
            
            button1.addActionListener(new ActionListener() { // 产生随机数
                public void actionPerformed(ActionEvent e) {
                    int a = (int) (1 + Math.random() * 100);
                    int b = (int) (1 + Math.random() * 100);
                    fuhao = choiceFuhao.getSelectedItem().toString();
                    String s = String.valueOf(a) + fuhao + String.valueOf(b);
                    text1.setText(s);
                }
            });
            
            button2.addActionListener(new ActionListener() { // 输入答案
                public void actionPerformed(ActionEvent e) {
                    String regex = "[^0123456789.]+"; // 将字符串s变为2个整数并相加得到sum
                    String T = text1.getText().replaceAll(regex, "#");
                    StringTokenizer fenxi = new StringTokenizer(T, "#");
                    int sum = 0, i = 0;
                    double chu = 0;
                    int[] t = new int[2];
                    while (fenxi.hasMoreTokens()) { // 进行四则运算,得到结果sum
                        String item = fenxi.nextToken();
                        t[i++] = (int) Double.parseDouble(item);
                    }
                    if (fuhao.equals("+"))
                        sum = t[0] + t[1];
                    else if (fuhao.equals("-"))
                        sum = t[0] - t[1];
                    else if (fuhao.equals("*"))
                        sum = t[0] * t[1];
                    else if (fuhao.equals("/"))
                        chu = ((double) t[0]) / ((double) t[1]);
    
                    if (text2.getText().equals(String.valueOf(sum)) || text2.getText().equals(String.format("%.2f", chu))) { // 判断输入的数字是否正确
                        JOptionPane.showMessageDialog(button2, "答案正确", "消息对话框", JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(button2, "答案错误", "消息对话框", JOptionPane.WARNING_MESSAGE);
                    }
                }
    
            });
            
            choiceFuhao.setBounds(120, 50, 120, 20);
            text1.setBounds(120, 100, 120, 20);
            text2.setBounds(120, 150, 120, 20);
            button1.setBounds(20, 100, 100, 20);
            button2.setBounds(20, 150, 100, 20);
            add(choiceFuhao);
            add(text1);
            add(text2);
            add(button1);
            add(button2);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    
    public class sizeyunsuan {
        public static void main(String args[]) {
            WindowActionEvent win = new WindowActionEvent();
            win.setBounds(100, 100, 400, 400);
            win.setTitle("100以内的四则运算测试");
            Container con = win.getContentPane();
            con.setBackground(new Color(178, 255, 242));
        }
    }

     初始界面

     输入的答案正确时

     答案错误时

  • 相关阅读:
    Entity SQL 初入
    ObjectQuery查询及方法
    Entity Framework 的事务 DbTransaction
    Construct Binary Tree from Preorder and Inorder Traversal
    Reverse Linked List
    Best Time to Buy and Sell Stock
    Remove Duplicates from Sorted Array II
    Reverse Integer
    Implement Stack using Queues
    C++中const限定符的应用
  • 原文地址:https://www.cnblogs.com/ghh0/p/15345651.html
Copyright © 2020-2023  润新知