1

我知道可能有更好的方法让这个程序做它做的事情,但是我想不出一个更好的算法来完全按照这个程序做,任何建议都是有帮助的

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("");
        }


}



}
}
4

1 回答 1

3

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

你在哪里执行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 创建字段。
处理刚刚输入的输入
   如果 input 是一个数字并且 leftValue 是 null
       左值 = 输入
   如果输入是一个数字并且leftValue(和运算符)不为空
       leftValue = leftValue 操作输入
   如果 input 是运算符并且 leftValue 不为 null
       运算符 = 输入
于 2013-07-09T05:17:29.053 回答