2017-02-14 52 views
0

我想在我的計算器程序上實現這個actionPerformed方法。我無法創建一個循環,它會顯示按鈕並在按下時執行計算。順便說一下,如果我通過使用if語句執行此操作,但我想使代碼更清晰,我可以解決我的問題。 這裏是我的代碼:使用循環執行actionPerformed(點擊按鈕)

 String expr1 = ""; 

     private static final String[] bText = {"7", "8", "9", "+", "4", "5", "6", "- ", "1", "2", "3", "*", "0", ".", "=", "/", "(", ")", "C", "CE"}; 
     Button[] buttons = new Button[ bText.length ]; 

     public void actionPerformed(ActionEvent arg0) { 

     String input = textField.getText(); 

     // Trying to get the first 12 items for testing 
     for(int index = 0; index <=12; index++) 
     { 
      if(arg0.getSource()==buttons[index]) 
      { 

       if(input.contains("1,2,3,4,5,6,7,8,9,0")) 
       { 
       textField.setText(input + bText[index]); 
       expr1 = expr1 + index; 
       } 
       if(input.contains("+-*/")){ 
        expr1 = expr1 + bText[index]; 
       textField.setText(""); 
       } 
      } 

     }  
      for(int i=13; i <=16; i++){ 
       if(arg0.getSource()==buttons[i]){ 
      expr1 = expr1 + bText[i]; 
       textField.setText(""); 

      } 
      } 
     /** 
      If I do this one by one using if statements I can fix my problem but I want to make my code cleaner. 

      */ 


      //For CE button 
      if (arg0.getSource() == buttons[18]) { 
      String s = textField.getText().toString(); 
      s = s.substring(0, s.length() - 1); 
      textField.setText(s); 

      } 
       else if(arg0.getSource()==buttons[17]) { 
       textField.setText(""); 
       expr1 = "";  
      } 
     // This will calculate expressins. This is a "=" button 
     else if (arg0.getSource() == buttons[19]) { 
       System.out.println(expr1); 
       textField.setText("" + Integer.toString(calculatorEvaluator.eval(expr1))); 
      } 


     } 
+1

請格式化你的代碼,所以它更容易閱讀和更好地解釋你的問題。你有什麼麻煩,爲什麼'if'有幫助? – ChiefTwoPencils

+0

你可以通過使用'JButton btn =(JButton)arg0.getSource();'從這個事件中訪問按鈕,你應該使用'getText'或'getActionCommand'來確定要執行的動作 – MadProgrammer

+0

我想知道我如何通過使用for循環來最小化代碼的長度。如果我通過使用if語句來做,我必須使用20個if語句,並且我想知道是否有更簡單的方法。即;循環。格式化抱歉,我會做。 – user7062312

回答

1

有很多我能想到的處理這個問題,使用Action的API的方式可能是我最優選的,但可能超出此要求的範圍。

我不認爲你需要使用循環,我不認爲他們會給你任何優勢超過其他方法。

例如,您可以使用構建於正則表達式支持中的String,並簡單地檢查按鈕的文本。

這,至少,讓你來決定,如果文本是一個數字或操作員或其他一些命令,例如...

private final String[] bText = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/", "(", ")", "C", "CE"}; 
private JButton[] buttons = new JButton[bText.length]; 

public TestPane() { 
    setLayout(new GridLayout(0, 3)); 
    for (int index = 0; index < bText.length; index++) { 
     String text = bText[index]; 
     JButton btn = new JButton(text); 
     buttons[index] = btn; 
     btn.addActionListener(this); 
     add(btn); 
    } 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    Object source = e.getSource(); 
    if (source instanceof JButton) { 
     JButton btn = (JButton)source; 
     String text = btn.getText(); 
     if (text.matches("^[0-9]")) { 
      System.out.println(text + " is a number"); 
     } else if (text.matches("^[=/\\(\\)*=\\-\\+]")) { 
      System.out.println(text + " is an operator"); 
     } else { 
      System.out.println(text + " is some other command"); 
     } 
    } 
}