【问题标题】:Calculator works with just 2 numbers - fails with more计算器仅适用于 2 个数字 - 更多则失败
【发布时间】:2026-01-20 12:55:01
【问题描述】:

我完全知道有更好的方法可以做到这一点,但只要它最终起作用,我就会非常满意,每当我执行 4+4 之类的操作时,它都是正确的,但如果我执行 4+4* 之类的操作2 等于 32

package calc;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
*
* @author Ben
*/
public class GUI extends JFrame {

    int response, operator;
    double num1, num2, total = 0;
    String operation, answer, num, testnum;
    private JButton one, two, three, four, five, six, seven, eight,
    nine, zero, multiply, divide, subtract, add, equals, clear;
    private JTextField display, fakedisplay;

    public GUI() {
        super("Calculator");
        setLayout(new FlowLayout());

        fakedisplay = new JTextField(10);
        display = new JTextField(10);;
        add(display);
        one = new JButton("1");
        add(one);
        two = new JButton("2");
        add(two);
        three = new JButton("3");
        add(three);
        four = new JButton("4");
        add(four);
        five = new JButton("5");
        add(five);
        six = new JButton("6");
        add(six);
        seven = new JButton("7");
        add(seven);
        eight = new JButton("8");
        add(eight);
        nine = new JButton("9");
        add(nine);
        zero = new JButton("0");
        add(zero);
        multiply = new JButton("*");
        add(multiply);
        divide = new JButton("/");
        add(divide);
        subtract = new JButton("-");
        add(subtract);
        add = new JButton("+");
        add(add);
        equals = new JButton("=");
        add(equals);
        clear = new JButton("Clear");
        add(clear);


        handler handle = new handler();

        one.addActionListener(handle);
        two.addActionListener(handle);
        three.addActionListener(handle);
        four.addActionListener(handle);
        five.addActionListener(handle);
        six.addActionListener(handle);
        seven.addActionListener(handle);
        eight.addActionListener(handle);
        nine.addActionListener(handle);
        zero.addActionListener(handle);
        multiply.addActionListener(handle);
        divide.addActionListener(handle);
        subtract.addActionListener(handle);
        add.addActionListener(handle);
        equals.addActionListener(handle);
        clear.addActionListener(handle);

    }
    private class handler implements ActionListener {

        @
        Override
        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == one) {
                response = 1;
                display.setText(display.getText() + 1);
                fakedisplay.setText(fakedisplay.getText() + 1);
            } else if(e.getSource() == two) {
                response = 2;
                display.setText(display.getText() + 2);
                fakedisplay.setText(fakedisplay.getText() + 2);
            } else if(e.getSource() == three) {
                response = 3;
                display.setText(display.getText() + 3);
                fakedisplay.setText(fakedisplay.getText() + 3);
            } else if(e.getSource() == four) {
                response = 4;
                display.setText(display.getText() + 4);
                fakedisplay.setText(fakedisplay.getText() + 4);
            } else if(e.getSource() == five) {
                response = 5;
                display.setText(display.getText() + 5);
                fakedisplay.setText(fakedisplay.getText() + 5);
            } else if(e.getSource() == six) {
                response = 6;
                display.setText(display.getText() + 6);
                fakedisplay.setText(fakedisplay.getText() + 6);
            } else if(e.getSource() == seven) {
                response = 7;
                display.setText(display.getText() + 7);
                fakedisplay.setText(fakedisplay.getText() + 7);
            } else if(e.getSource() == eight) {
                response = 8;
                display.setText(display.getText() + 8);
                fakedisplay.setText(fakedisplay.getText() + 8);
            } else if(e.getSource() == nine) {
                response = 9;
                display.setText(display.getText() + 9);
                fakedisplay.setText(fakedisplay.getText() + 9);
            } else if(e.getSource() == zero) {
                response = 0;
                display.setText(display.getText() + 0);
                fakedisplay.setText(fakedisplay.getText() + 0);
            } else if(e.getSource() == multiply) {
                if(operator == 0 && num1 == 0) {
                    num1 = Double.parseDouble(display.getText());
                    operator = 3;
                    fakedisplay.setText("");
                } else if(operator != 0) {
                    operator = 3;
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    num2 = 0;
                    total = 0;
                    fakedisplay.setText("");
                    operator = 0;
                }
                operation = "*";
                display.setText(display.getText() + operation);

            } else if(e.getSource() == divide) {
                if(operator == 0 && num1 == 0) {
                    num1 = Double.parseDouble(display.getText());
                    operator = 4;
                    fakedisplay.setText("");
                } else if(total == 0) {
                    operator = 3;
                } else if(operator != 0) {
                    operator = 4;
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    num2 = 0;
                    total = 0;
                    fakedisplay.setText("");
                    operator = 0;

                }
                operation = "/";
                display.setText(display.getText() + operation);

            } else if(e.getSource() == add) {
                if(operator == 0 && num1 == 0) {
                    num1 = Double.parseDouble(display.getText());
                    operator = 1;
                    fakedisplay.setText("");
                } else if(operator != 0) {
                    operator = 1;
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    num2 = 0;
                    total = 0;
                    fakedisplay.setText("");
                    operator = 0;
                }
                operation = "+";
                display.setText(display.getText() + operation);

            } else if(e.getSource() == subtract) {

                if(operator == 0 && num1 == 0) {
                    num1 = Double.parseDouble(display.getText());
                    operator = 2;
                    fakedisplay.setText("");
                } else if(operator != 0) {
                    operator = 2;
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    num2 = 0;
                    total = 0;
                    fakedisplay.setText("");
                    operator = 0;
                }
                operation = "-";
                display.setText(display.getText() + operation);

            } else if(e.getSource() == equals) {
                if(operator == 0) {
                    display.setText("Error");
                } else if(operator != 0) {
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + "=" + total);
                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + "=" + total);
                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + "=" + total);
                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + "=" + total);
                    }

                }
                operation = "=";

            } else if(e.getSource() == clear) {
                display.setText("");
                fakedisplay.setText("");
                operator = 0;
                total = 0;
                num1 = 0;
                num2 = 0;


            }
            System.out.println("num1: " + num1 + " num2: " + num2 + "\ndisplay: " + display.getText() + " fakedisplay: " + fakedisplay.getText() + "\nresponse: " + response + "\noperator: " + operator + "\ntotal: " + total + "\n==========================");

        }



    }
}

【问题讨论】:

  • 然后你需要添加一个“计算”或“=”按钮,让它看到所有具有相关优先级的运算符。就像编写一个迷你编译器的解析器一样。执行 +-/* 会立即更改计算顺序。你需要优先级。所以请将计算线移动到“=”按钮。
  • 8*2 是一个不好的例子,它采用第二个运算符并将其用于第一个运算符,这与只有 2 个数字时不同。
  • 欢迎来到 Stack Overflow。请注意,这不是“请调试我的代码”站点。人们在这里提出问题并获得答案。您的帖子不能称为“问题”。
  • 您可以编辑代码以便我完全理解吗? @huseyin
  • 除非有规范以特定方式执行(例如学习目的),否则我将使用 ScriptEngine 来实现此功能。例如。 Calculet.

标签: java swing user-interface jtextfield


【解决方案1】:

我只能指导你一些网上资料,你需要进行解析。将最终命令字符串的每个标记(+-*/)放在一个列表中(也许二叉树可以工作)并对该列表进行排序,以告诉计算器除法和乘法的优先级最高,而加法和减法的优先级较低。

您以这样的方式对命令数组进行排序,即左侧优先于右侧。然后它从左到右开始计算,同时先选择 div/mult。

http://www.slideshare.net/dabeaz/writing-parsers-and-compilers-with-ply

http://parsingintro.sourceforge.net/

http://www.dreamincode.net/forums/topic/268945-an-introduction-to-compiler-design-part-ii-parsing/

https://en.wikipedia.org/wiki/Parsing

http://arantxa.ii.uam.es/~modonnel/Compilers/03_1_Parsing_Intro.pdf

如果你太累了,可以使用script engine of java,来自用户“Andrew Thompson”的建议。

【讨论】:

    【解决方案2】:

    我想你可能会变得有点不同。您正在设计一个复杂的解决方案 对于一个非常简单的问题。

    1. First of all Use  a single JTextField for the user to enter the entire expression.
       i.e. [ 4+4*2].
    
    2. Second Provide a Submit Buttons( jbutton ) along with text field. 
       Now when the User is finished with entering expression he will click the submit button.
    
       i.e [4+4*2] [Click to Submit]
    

    不,您为此按钮编写处理程序。 在处理程序中,只需从 文本域 并跟随 反向波兰表示法来评估您的结果。 它非常简单的基于堆栈的算法。通常在数据结构课程中教授。 看这篇文章http://en.wikipedia.org/wiki/Reverse_Polish_notation

    【讨论】:

      最近更新 更多