如何通过使用split string()在GUI中为科学计算器的等号按钮编写代码?

时间:2019-05-02 19:50:51

标签: java

我正在尝试用Java(GUI)编写科学计算器。如何为等号按钮编写命令以考虑字符串中*,/,+,-的优先级?

    String[] st = s.split("(?<=[-+*/])|(?=[-+*/])");
            char eql[] = s.toCharArray();
    for (int i = 0; i <= st.length; i++) {
        if ('*' == eql[i]) {
            double a = eql[i + 1] * eql[i - 1];
            jTextField1.setText(a+"");
        }

1 个答案:

答案 0 :(得分:0)

if (input is * || input is /)
    //do multiplication and division left to right
else if (input is + || input is -)
    //do addition and subtraction left to right

我还要补充一点,Shunting Yard Algorithm可能有用或有趣。希望这会有所帮助!