2011-12-04 94 views
2

對我的程序有點麻煩,它顯示錯誤的結果,並且當您嘗試對單個數字進行計算時,您將看到一個錯誤。計劃發佈如下,任何幫助表示讚賞。Java計算器顯示不正確的結果

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

} 
+1

* 「你會得到一個錯誤。」 *什麼錯誤?總是複製/粘貼錯誤輸出,而不是讓我們猜測。順便說一句,不要忘記提問。 –

+0

在您的代碼中放置更多println,特別是在出現錯誤之前。然後從邏輯上瀏覽代碼,就好像你是電腦一樣。然後你會看到你不會像你認爲你正在做的那樣改變顯示(並且通過擴展display1)。 –

回答

1

你需要從jtfResult獲取當前值。

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

你的問題是在這裏

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

變化

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

而且,只要任何操作者點擊你要清楚你的顯示字符串。

在附註中,您應該儘量避免編寫重複的代碼塊,您使用哪種解決方案來解決您的問題,您將在很多地方更改代碼。

你可以有你的actionPerformed這樣的:

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