I';我用java创建了一个GUI计算器,看起来变量不是';t保持最新状态,保持在0

I';我用java创建了一个GUI计算器,看起来变量不是';t保持最新状态,保持在0,java,swing,jbutton,calculator,Java,Swing,Jbutton,Calculator,我知道可能有更好的方法让这个程序做它所做的事情,但我不能想出一个更好的算法来精确地做这个程序,任何建议都是有用的 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.JTe

我知道可能有更好的方法让这个程序做它所做的事情,但我不能想出一个更好的算法来精确地做这个程序,任何建议都是有用的

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);
这将提示您在按下每个键/按钮后发生了什么

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

Create fields for leftValue, operator, rightValue. Handle the input just entered if input is a number and leftValue is null leftValue = input if input is a number and leftValue (and operator) are not null leftValue = leftValue OPERATION input if input is an operator and leftValue is not null operator = input 为leftValue、operator和rightValue创建字段。 处理刚输入的输入 如果输入为数字且leftValue为空 leftValue=输入 如果输入为数字且leftValue(和运算符)不为null leftValue=leftValue操作输入 如果输入为运算符且leftValue不为null 操作员=输入
请参考这一点,这可能会有所帮助:-)我没有仔细阅读200多行的源代码(至少没有仔细阅读),但请注意,在140 LOC左右实现了一个可工作的计算器。感谢帮助,在我添加代码后,该代码确实显示了背景中发生的事情谢谢!我仍然有点困惑如何才能让count准确地上升,因为count==0应该可以工作,因为它的默认值是0,然后在if语句的末尾是count++,所以它不应该把它上升到1吗?是的,但是一旦它达到1,它就不会回到循环的(做一些操作)部分。如果执行“2”+“4”=,则将在“+”之后单击count=1,但不会重新进入该循环(当count=1时)以执行该操作。我认为您需要考虑当您点击“=”时会发生什么,并将其视为一个操作。抱歉,如果我听起来好像什么都不知道,因为我刚刚开始进入GUI和actionListener,但是当您有actionListener时,这是否意味着您必须以某种方式使程序运行,以保持像您上面所述的那样循环?就像让它在计数等于1时继续运行一样。
ActionListener
侦听操作(例如按下按钮),因此每次有操作时,侦听器都会查询操作
(如果e.getSource()==无论什么)
,以查看它是否能够处理它。观察当你做3x3x3=时会发生什么。另外,在'count'和''之间的
System.out.println
中添加:`+'\n总计:“+total`”。我已经编辑了答案来添加这个。是的,它给变量分配了相同的数字,那么是否有其他类型的监听器,而不是ActionListener?