zjw_yyds 2022-06-08 10:17 采纳率: 100%
浏览 45
已结题

java简易计算器代码问答

问题遇到的现象和发生背景

麻烦您帮忙给我这段计算器代码详细的注释一下,感谢感谢

问题相关代码,请勿粘贴截图

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Calcultor215 {
TextField tf = new TextField(40);
StringBuffer s1 = new StringBuffer();
StringBuffer s2 = new StringBuffer();
static String flag = new String();
boolean start = true;

public Calcultor215() {
}

public void init() {
    JFrame f = new JFrame("计算器");
    JPanel p1 = new JPanel();
    p1.add(this.tf);
    f.add(p1, "North");
    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(1, 3));
    ActionListener listen = new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (arg0.getActionCommand() == "0" && !Calcultor215.this.s1.toString().equals(new String())) {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("0").toString());
            }

            if (arg0.getActionCommand() == "1") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("1").toString());
            }

            if (arg0.getActionCommand() == "2") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("2").toString());
            }

            if (arg0.getActionCommand() == "3") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("3").toString());
            }

            if (arg0.getActionCommand() == "4") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("4").toString());
            }

            if (arg0.getActionCommand() == "5") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("5").toString());
            }

            if (arg0.getActionCommand() == "6") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("6").toString());
            }

            if (arg0.getActionCommand() == "7") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("7").toString());
            }



            

            if (arg0.getActionCommand() == "8") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("8").toString());
            }

            if (arg0.getActionCommand() == "9") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append("9").toString());
            }

            if (arg0.getActionCommand() == ".") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                if (Calcultor215.this.s1.toString().equals(new String())) {
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.append("0.").toString());
                } else if (Calcultor215.this.s1.length() == 1 && Calcultor215.this.s1.charAt(0) == '-') {
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.append("0.").toString());
                } else {
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.append(".").toString());
                }
            }

            if (arg0.getActionCommand() == "+/-") {
                if (!Calcultor215.this.start) {
                    Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                }

                Calcultor215.this.start = true;
                if (Calcultor215.this.s1.toString().equals(new String())) {
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.append('-').toString());
                }
            }

            if (arg0.getActionCommand() == "Backspace") {
                Calcultor215.this.start = true;
                if (Calcultor215.this.s1.length() != 0) {
                    Calcultor215.this.s1.deleteCharAt(Calcultor215.this.s1.length() - 1);
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
                }
            }

            if (arg0.getActionCommand() == "CE") {
                Calcultor215.this.start = true;
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.s2.delete(0, Calcultor215.this.s2.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
            }

            if (arg0.getActionCommand() == "C") {
                Calcultor215.this.start = true;
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
            }

            if (arg0.getActionCommand() == "+") {
                Calcultor215.this.start = true;
                Calcultor215.flag = "+";
                Calcultor215.this.s2.append(String.valueOf(Calcultor215.this.s1.toString()));
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
            }

            if (arg0.getActionCommand() == "-") {
                Calcultor215.this.start = true;
                Calcultor215.flag = "-";
                Calcultor215.this.s2.append(String.valueOf(Calcultor215.this.s1.toString()));
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
            }

            if (arg0.getActionCommand() == "*") {
                Calcultor215.this.start = true;
                Calcultor215.flag = "*";
                Calcultor215.this.s2.append(String.valueOf(Calcultor215.this.s1.toString()));
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
            }

            if (arg0.getActionCommand() == "/") {
                Calcultor215.this.start = true;
                Calcultor215.flag = "/";
                Calcultor215.this.s2.append(String.valueOf(Calcultor215.this.s1.toString()));
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
            }

            double s4;
            if (arg0.getActionCommand() == "sqrt") {
                Calcultor215.this.start = false;
                s4 = Double.parseDouble(Calcultor215.this.s1.toString());
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append(String.valueOf(Math.sqrt(s4))).toString());
            }

            if (arg0.getActionCommand() == "%") {
                Calcultor215.this.start = false;
                s4 = Double.parseDouble(Calcultor215.this.s1.toString());
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append(String.valueOf(s4 / 100.0D)).toString());
            }

            if (arg0.getActionCommand() == "1/x" && !Calcultor215.this.s1.toString().equals(new String()) && Double.parseDouble(Calcultor215.this.s1.toString()) != 0.0D) {
                Calcultor215.this.start = false;
                s4 = Double.parseDouble(Calcultor215.this.s1.toString());
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.tf.setText(Calcultor215.this.s1.append(String.valueOf(1.0D / s4)).toString());
            }

            if (arg0.getActionCommand() == "=" && Calcultor215.this.s1.length() != 0 && Calcultor215.this.s2.length() != 0) {
                Calcultor215.this.start = false;
                s4 = Double.parseDouble(Calcultor215.this.s1.toString());
                double s3 = Double.parseDouble(Calcultor215.this.s2.toString());
                Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                Calcultor215.this.s2.delete(0, Calcultor215.this.s2.length());
                if (Calcultor215.flag == "+") {
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.append(String.valueOf(s3 + s4)).toString());
                }

                if (Calcultor215.flag == "-") {
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.append(String.valueOf(s3 - s4)).toString());
                }

                if (Calcultor215.flag == "*") {
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.append(String.valueOf(s3 * s4)).toString());
                }

                if (Calcultor215.flag == "/") {
                    Calcultor215.this.tf.setText(Calcultor215.this.s1.append(String.valueOf(s3 / s4)).toString());
                }
            }

        }
    };
    JButton bks = new JButton("Backspace");
    bks.addActionListener(listen);
    JButton ce = new JButton("CE");
    ce.addActionListener(listen);
    JButton c = new JButton("C");
    c.addActionListener(listen);
    p2.add(bks);
    p2.add(ce);
    p2.add(c);
    f.add(p2);
    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(4, 5));
    JButton b1 = new JButton("7");
    p3.add(b1);
    b1.addActionListener(listen);
    JButton b2 = new JButton("8");
    p3.add(b2);
    b2.addActionListener(listen);
    JButton b3 = new JButton("9");
    p3.add(b3);
    b3.addActionListener(listen);
    JButton b4 = new JButton("/");
    p3.add(b4);
    b4.addActionListener(listen);
    JButton b5 = new JButton("sqrt");
    p3.add(b5);
    b5.addActionListener(listen);
    JButton b6 = new JButton("4");
    p3.add(b6);
    b6.addActionListener(listen);
    JButton b7 = new JButton("5");
    p3.add(b7);
    b7.addActionListener(listen);
    JButton b8 = new JButton("6");
    p3.add(b8);
    b8.addActionListener(listen);
    JButton b9 = new JButton("*");
    p3.add(b9);
    b9.addActionListener(listen);
    JButton b10 = new JButton("%");
    p3.add(b10);
    b10.addActionListener(listen);
    JButton b11 = new JButton("1");
    p3.add(b11);
    b11.addActionListener(listen);
    JButton b12 = new JButton("2");
    p3.add(b12);
    b12.addActionListener(listen);
    JButton b13 = new JButton("3");
    p3.add(b13);
    b13.addActionListener(listen);
    JButton b14 = new JButton("-");
    p3.add(b14);
    b14.addActionListener(listen);
    JButton b15 = new JButton("1/x");
    p3.add(b15);
    b15.addActionListener(listen);
    JButton b16 = new JButton("0");
    p3.add(b16);
    b16.addActionListener(listen);
    JButton b17 = new JButton("+/-");
    p3.add(b17);
    b17.addActionListener(listen);
    JButton b18 = new JButton(".");
    p3.add(b18);
    b18.addActionListener(listen);
    JButton b19 = new JButton("+");
    p3.add(b19);
    b19.addActionListener(listen);
    JButton b20 = new JButton("=");
    p3.add(b20);
    b20.addActionListener(listen);
    f.add(p3, "South");
    f.setDefaultCloseOperation(2);
    f.pack();
    f.setVisible(true);
}

public static void main(String[] args) {
    (new Calcultor215()).init();
}

}

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 小秋Kaito 2022-06-08 11:24
    关注
    package com.test;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    
    public class Calcultor215 {
        // 数字输入框
        TextField tf = new TextField(40);
        // 运算的第一个数
        StringBuffer s1 = new StringBuffer();
        // 运算的第二个数
        StringBuffer s2 = new StringBuffer();
        // 运算符标志位
        static String flag = "";
        // 标志位,用于记录是否仍在继续输入数字或是运算符输入
        boolean start = true;
    
        public Calcultor215() {
        }
    
        public void init() {
            JFrame f = new JFrame("计算器");
            JPanel p1 = new JPanel();
            p1.add(this.tf);
            f.add(p1, "North");
            JPanel p2 = new JPanel();
            p2.setLayout(new GridLayout(1, 3));
            // 监听器,监听按钮点击事件
            ActionListener listen = arg0 -> {
                double s4;
                // 原代码用大量if进行判断,此处用switch-case进行优化
                switch (arg0.getActionCommand()){
                    // 数字键点击0-9
                    case "0":
                        // 不响应首位0
                        if ("".equals(Calcultor215.this.s1.toString())){
                            break;
                        }
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                        if (!Calcultor215.this.start) {
                            Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                        }
    
                        Calcultor215.this.start = true;
                        Calcultor215.this.tf.setText(Calcultor215.this.s1.append(arg0.getActionCommand()).toString());
                        break;
                    // 小数点点击
                    case ".":
                        if (!Calcultor215.this.start) {
                            Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                        }
    
                        Calcultor215.this.start = true;
                        if ("".equals(Calcultor215.this.s1.toString())) {
                            Calcultor215.this.tf.setText(Calcultor215.this.s1.append("0.").toString());
                        } else if (Calcultor215.this.s1.length() == 1 && Calcultor215.this.s1.charAt(0) == '-') {
                            Calcultor215.this.tf.setText(Calcultor215.this.s1.append("0.").toString());
                        } else {
                            Calcultor215.this.tf.setText(Calcultor215.this.s1.append(".").toString());
                        }
                        break;
                    // 正负号点击
                    case "+/-":
                        if (!Calcultor215.this.start) {
                            Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                        }
    
                        Calcultor215.this.start = true;
                        if ("".equals(Calcultor215.this.s1.toString())) {
                            Calcultor215.this.tf.setText(Calcultor215.this.s1.append('-').toString());
                        }
                    // 退格
                    case "Backspace":
                        Calcultor215.this.start = true;
                        if (Calcultor215.this.s1.length() != 0) {
                            Calcultor215.this.s1.deleteCharAt(Calcultor215.this.s1.length() - 1);
                            Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
                        }
                        break;
                    // 清除,删除两个输入的数
                    case "CE":
                        Calcultor215.this.start = true;
                        Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                        Calcultor215.this.s2.delete(0, Calcultor215.this.s2.length());
                        Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
                        break;
                    // 清除,删除第一个输入的数
                    case "C":
                        Calcultor215.this.start = true;
                        Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                        Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
                        break;
                    // 四则运算点击,状态存入运算符标志位flag
                    case "+":
                    case "-":
                    case "*":
                    case "/":
                        Calcultor215.this.start = true;
                        Calcultor215.flag = arg0.getActionCommand();
                        Calcultor215.this.s2.append(Calcultor215.this.s1.toString());
                        Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                        Calcultor215.this.tf.setText(Calcultor215.this.s1.toString());
                        break;
                    // 将当前输入的数开方
                    case "sqrt":
                        Calcultor215.this.start = false;
                        s4 = Double.parseDouble(Calcultor215.this.s1.toString());
                        Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                        Calcultor215.this.tf.setText(Calcultor215.this.s1.append(Math.sqrt(s4)).toString());
                        break;
                    // 求百分比
                    case "%":
                        Calcultor215.this.start = false;
                        s4 = Double.parseDouble(Calcultor215.this.s1.toString());
                        Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                        Calcultor215.this.tf.setText(Calcultor215.this.s1.append(s4 / 100.0D).toString());
                        break;
                    // 求1除以当前输入数的结果
                    case "1/x":
                        if (!"".equals(Calcultor215.this.s1.toString()) && Double.parseDouble(Calcultor215.this.s1.toString()) != 0.0D){
                            Calcultor215.this.start = false;
                            s4 = Double.parseDouble(Calcultor215.this.s1.toString());
                            Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                            Calcultor215.this.tf.setText(Calcultor215.this.s1.append(1.0D / s4).toString());
                        }
                        break;
                    // 进行计算
                    case "=":
                        if (Calcultor215.this.s1.length() != 0 && Calcultor215.this.s2.length() != 0){
                            Calcultor215.this.start = false;
                            s4 = Double.parseDouble(Calcultor215.this.s1.toString());
                            double s3 = Double.parseDouble(Calcultor215.this.s2.toString());
                            Calcultor215.this.s1.delete(0, Calcultor215.this.s1.length());
                            Calcultor215.this.s2.delete(0, Calcultor215.this.s2.length());
                            if ("+".equals(Calcultor215.flag)) {
                                Calcultor215.this.tf.setText(Calcultor215.this.s1.append((s3 + s4)).toString());
                            }
    
                            if ("-".equals(Calcultor215.flag)) {
                                Calcultor215.this.tf.setText(Calcultor215.this.s1.append((s3 - s4)).toString());
                            }
    
                            if ("*".equals(Calcultor215.flag)) {
                                Calcultor215.this.tf.setText(Calcultor215.this.s1.append(s3 * s4).toString());
                            }
    
                            if ("/".equals(Calcultor215.flag)) {
                                Calcultor215.this.tf.setText(Calcultor215.this.s1.append(s3 / s4).toString());
                            }
                        }
                        break;
                    default:
                }
            };
    
            // 以下直至结束都是在构建计算器UI
            JButton bks = new JButton("Backspace");
            bks.addActionListener(listen);
            JButton ce = new JButton("CE");
            ce.addActionListener(listen);
            JButton c = new JButton("C");
            c.addActionListener(listen);
            p2.add(bks);
            p2.add(ce);
            p2.add(c);
            f.add(p2);
            JPanel p3 = new JPanel();
            p3.setLayout(new GridLayout(4, 5));
    
            // 原代码,此处构建UI有大量重复代码,简单优化用循环减少代码长度
            String[] list = new String[]{"7", "8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
            for (String s : list){
                JButton b = new JButton(s);
                p3.add(b);
                b.addActionListener(listen);
            }
    
            f.add(p3, "South");
            f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            f.pack();
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            (new Calcultor215()).init();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月23日
  • 已采纳回答 6月15日
  • 创建了问题 6月8日

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。