Java计算器显示不正确的结果

时间:2011-12-04 05:02:13

标签: java swing runtime-error calculator

我的程序有点麻烦,它显示错误的结果,当你尝试对一个数字进行计算时,你会收到错误。程序发布在下面,任何帮助表示赞赏。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calc extends JFrame{
public static void main(String[] args){
    Calc myFrame = new Calc();
    myFrame.pack();
    myFrame.setTitle("Calculator");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setLocationRelativeTo(null);
    myFrame.setVisible(true);
}//main


private JTextField jtfNum1, jtfNum2, jtfResult;
private JButton jbtAdd, jbtSub, jbtMul, jbtDiv, jbt1, jbt2, jbt3, jbt4, jbt5, jbt6, jbt7, jbt8, jbt9, jbt0, jbtDec, jbtC, jbtE, jbtClear;
double number1 = 0;
double number2 = 0;
double result = 0;
String display = "";
String display1 ="";
char oper = ('A');

public Calc(){
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(5,4));
    p1.add(jbt1 = new JButton("1"));
    p1.add(jbt2 = new JButton("2"));
    p1.add(jbt3 = new JButton("3"));
    p1.add(jbtAdd = new JButton("+"));
    p1.add(jbt4 = new JButton("4"));
    p1.add(jbt5 = new JButton("5"));
    p1.add(jbt6 = new JButton("6"));
    p1.add(jbtSub = new JButton("-"));
    p1.add(jbt7 = new JButton("7"));
    p1.add(jbt8 = new JButton("8"));
    p1.add(jbt9 = new JButton("9"));
    p1.add(jbtMul = new JButton("*"));
    p1.add(jbt0 = new JButton("0"));
    p1.add(jbtClear = new JButton("CE"));
    p1.add(jbtE = new JButton("="));
    p1.add(jbtDiv = new JButton("/"));

    JPanel p2 = new JPanel(new BorderLayout());
    p2.add(jtfResult = new JTextField(8), BorderLayout.NORTH);
    p2.add(p1, BorderLayout.CENTER);
    jtfResult.setEditable(false);

    this.setLayout(new BorderLayout());
    this.add(p2, BorderLayout.CENTER);

    jbt1.addActionListener(new myListener());
    jbt2.addActionListener(new myListener());
    jbt3.addActionListener(new myListener());
    jbt4.addActionListener(new myListener());
    jbt5.addActionListener(new myListener());
    jbt6.addActionListener(new myListener());
    jbt7.addActionListener(new myListener());
    jbt8.addActionListener(new myListener());
    jbt9.addActionListener(new myListener());
    jbt0.addActionListener(new myListener());
    jbtAdd.addActionListener(new myListener());
    jbtDiv.addActionListener(new myListener());
    jbtSub.addActionListener(new myListener());
    jbtMul.addActionListener(new myListener());
    jbtE.addActionListener(new myListener());
    jbtClear.addActionListener(new myListener());

}

class myListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        String actionCommand = e.getActionCommand();
        if ("1".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "1");
        }
        else if ("2".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "2");
        }
        else if ("3".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "3");
        }
        else if ("4".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "4");
        }
        else if ("5".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "5");
        }
        else if ("6".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "6");
        }
        else if ("7".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "7");
        }
        else if ("8".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "8");
        }
        else if ("9".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "9");
        }
        else if ("0".equals(actionCommand)){
        display = jtfResult.getText();
        jtfResult.setText(display + "0");
        }
        else if ("+".equals(actionCommand)){
        display1 = display;
        oper = '+';
        jtfResult.setText("");
        }
        else if ("-".equals(actionCommand)){
        oper = '-';
        display1 = display;
        jtfResult.setText("");
        }
        else if ("/".equals(actionCommand)){
        oper = '/';
        display1 = display;
        jtfResult.setText("");
        }
        else if ("*".equals(actionCommand)){
        oper = '*';
        display1 = display;
        jtfResult.setText("");
        }
        else if("=".equals(actionCommand)){
            solve();
            System.out.println(display1);
            System.out.println(display);
        }
        else if ("CE".equals(actionCommand)){
        jtfResult.setText("");
        }
    }//Action Performed
}

public void solve(){
number1 = Double.parseDouble(display1);
number2 = Double.parseDouble(display);
jtfResult.setText("");
if (oper == '+'){
    result = number1 + number2;
    String stringResult = Double.toString(result);
    jtfResult.setText(stringResult);
    }
else if (oper == '-'){
    result = number1 - number2;
    String stringResult = Double.toString(result);
    jtfResult.setText(stringResult);
    }
else if (oper == '*'){
    result = number1 * number2;
    String stringResult = Double.toString(result);
    jtfResult.setText(stringResult);
    }
else if (oper == '/'){
    if (number2 == 0)
        jtfResult.setText("ERROR");
    else    
    result = number1 / number2;
    String stringResult = Double.toString(result);
    jtfResult.setText(stringResult);    
    }
}

}

2 个答案:

答案 0 :(得分:1)

您需要从jtfResult获取当前值。

public void actionPerformed(ActionEvent e){
        display = jtfResult.getText();  //<--------
        String actionCommand = e.getActionCommand();
        .....

答案 1 :(得分:1)

您的问题在这里

display = jtfResult.getText();
jtfResult.setText(display + "1");

将其更改为

jtfResult.setText(display + "1");
display = jtfResult.getText();

无论何时单击任何操作员,您都必须清除显示字符串。

在旁注中,您应该尽量避免编写重复的代码块,这是您用来修复问题的解决方案,您将更改代码这么多地方。

你可以像这样执行你的行动:

class myListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if ("=".equals(actionCommand)) {
            solve();
            System.out.println(display1);
            System.out.println(display);
        } else if ("CE".equals(actionCommand)) {
            jtfResult.setText("");
        } else {
            processInputReceived(actionCommand);
        }
    }// Action Performed
}

你的processInputReceived就像

private void processInputReceived(String actionCommand){
    if(Character.isDigit(actionCommand.toCharArray()[0])){
        digitsSelected(actionCommand);
    } else {
        operatorSelected(actionCommand.toCharArray()[0]);
    }
}

private void operatorSelected(char selectedOperator){
    oper = selectedOperator;
    display1 = display;
    display = "";
    jtfResult.setText("");
}

private void digitsSelected(String selectedValue){
    jtfResult.setText(display + selectedValue);
    display = jtfResult.getText();
}