我知道可能有一种更好的方法可以使该程序执行其工作,但是我无法提出一种更好的算法来准确执行该程序的工作,任何建议都将有所帮助

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, count=0;
double num1, num2, num3, num5, num6, num7, num8, 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);
    display.setEditable(false);
    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() + response);
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==two){
            response = 2;
            display.setText(display.getText() + response);
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==three){
                response = 3;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==four){
                response = 4;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==five){
                response = 5;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==six){
                response = 6;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==seven){
                response = 7;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==eight){
                response = 8;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==nine){
                response = 9;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if(e.getSource()==zero){
            response = 0;
            display.setText(display.getText() + response);
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==multiply){
            if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
            else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1*num2;
                count++;
            }
            else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total*num3;
                count++;
            }
            operation = "*";
             display.setText(display.getText() + operation);
             fakedisplay.setText("");

        }
        else if (e.getSource()==divide){
             if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
             else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1/num2+num1%num2;
                count++;
            }
             else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total/num3+total%num3;
                count++;
            }
                operation = "/";
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==add){
            if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
            else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1+num2;
                count++;
            }
            else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total+num3;
                count++;
            }
                operation = "+";
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==subtract){
             if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
             else if (count == 1) {
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1-num2;
                count++;
            }
             else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total-num3;
                count++;
            }
                operation = "-";
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==equals){
            operation = "=";
            display.setText(display.getText() + operation + total);
        }
        else if (e.getSource()==clear){
            display.setText("");
        }


}



}
}

最佳答案

我看到了几个问题,但是您首先需要帮助您:

您在哪里执行equals操作?做一个简单的算术函数:

A op B = ?


(其中A和B是数字,而op是+,-,*,/之一)

会将数字插入num1,但不会执行op操作,因为count永远不会等于1。

为了帮助自己,请在最后一个else if块之后添加以下内容:

System.out.println("num1: " + num1 + " num2: " + num2 + " num3: " + num3
   + "\ndisplay: " + display.getText() + " fakedisplay: " + fakedisplay.getText()
   + "\nresponse: " + response + "\ncount: " + count + "\ntotal: " + total);


这将为您提供有关您在按下每个键/按钮后发生了什么的提示。

如果考虑逻辑步骤,您需要做的是:

为leftValue,operator,rightValue创建字段。
处理刚输入的输入
   如果输入是一个数字并且leftValue为null
       leftValue =输入
   如果输入是一个数字,并且leftValue(和运算符)不为null
       leftValue = leftValue操作输入
   如果输入是一个运算符,并且leftValue不为null
       运算符=输入

07-27 16:59