zjw_yyds
2022-06-08 10:17
采纳率: 100%
浏览 24

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条回答 默认 最新

相关推荐 更多相似问题