2014-10-06 52 views
-2

我很確定我已經看過大多數與此問題有關的問題,但他們都使用多個文本字段,每個按鈕一個偵聽器或類似的東西。我試圖從一個文本字段獲取多個雙輸入,第一個變量設置爲初始文本,第二個變量設置爲第二個文本。所以如果有人輸入1 + 4,第一個變量設置爲1,+清除文本字段,第二個變量設置爲4.這是第一次寫入,所以可能會有很多不必要的事情。從一個JTextField解析多個雙打

編輯:可能在這裏不清楚,我想要發生的是用戶輸入(對於1 + 4),單擊按鈕1將文本字段設置爲1並將第一個變量設置爲1,然後單擊+按鈕,清除文本字段,然後單擊4按鈕將文本字段設置爲4並將第二個變量設置爲4,然後單擊等號按鈕,結果基於操作員單擊。它不是作爲一個字符串輸入的。

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

class GUICalculator extends JFrame implements ActionListener { 

    private static final int WIDTH = 300; 
    private static final int HEIGHT = 300; 
    private JTextField displayField; 
    private static final String[] NUMBERS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "C", "=", "."}; 
    private static final Set<String> OPERATORS = new HashSet<>(Arrays.asList("+", "-", "*", "/", "C", "=")); 
    private double n1, n2, result; 

    public GUICalculator() { 
     super("Calculator"); 
     setLayout(new BorderLayout()); 
     setSize(WIDTH, HEIGHT); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new FlowLayout()); 
     displayField = new JTextField(); 
     displayField.setEditable(false); 
     add(displayField, BorderLayout.NORTH); 
     add(buttonPanel); 

     for (String number : NUMBERS) { 
      JButton buttons = new JButton(number); 
      buttons.addActionListener(this); 
      buttonPanel.add(buttons); 
     } 
    } 

    public void actionPerformed(ActionEvent e) { 
     String command = e.getActionCommand(); 

     for (String number : NUMBERS) { 
      if (command.equals(number) && !OPERATORS.contains(command)) { 
       displayField.setText(displayField.getText() + command); 
       n1 = Double.parseDouble(displayField.getText()); 
      } 

      //n2 = Double.parseDouble(displayField.getText()); 

      switch (command) { 
       case "+": 
        result = n1 + n2; 
        displayField.setText(""); 
       case "-": 
        result = n1 - n2; 
        displayField.setText(""); 
       case "*": 
        result = n1 * n2; 
        displayField.setText(""); 
       case "/": 
        try { 
         if (n2 == 0) 
          throw new DivisionByZeroException(); 
         result = n1/n2; 
        } catch (DivisionByZeroException f) { 
         f.getMessage(); 
         displayField.setText(""); 
         break; 
        } 
       case "=": 
        displayField.setText(Double.toString(result)); 
        break; 
       case "C": 
        displayField.setText(""); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     GUICalculator gc = new GUICalculator(); 
     gc.setVisible(true); 
    } 
} 

註釋行是我想要進去的,但我不知道該把它放在哪裏。

+0

請參見[此答案](http://stackoverflow.com/a/7441804/418556)工作源使用['ScriptEngine'](http://docs.oracle.com/javase/8/docs/ api/javax/script/ScriptEngine.html)來評估表達式。 – 2014-10-06 07:36:03

回答

1

問題是,Double.parseDouble試圖解析整個字符串,而您試圖讀取字符串作爲一組標記。這需要實現一個表達式分析器。你的代碼看起來有點像解析器,但需要一種標記字符串的方法。

你可以看看解釋如this tutorial

一個額外的評論:請終止您的case代碼塊與break -statement。如果您的代碼與+匹配,則它將執行所有cases中的操作。因此,編寫1 + 2仍然會產生1/2的結果,並執行=的操作,因爲case確實有break語句。欲瞭解更多信息,請參閱Oracle info on the switch statement