2017-08-28 82 views
1

我的代碼的工作方式也是我想要的,但它使用了很多行。在我的Java代碼中更好地使用for循環

它關於一個食品訂單終端和我告訴你的部分是關於從購物車中刪除 JButtons。

我不想編輯每一個在Arraylist中的JButton。我認爲一個循環可以幫助在這裏,但我不知道如何做到這一點。

素不相識

public class Bestellterminal { 

private int x = 0; 
private double Preis = 0; 
private double classicpreis = 2.5; 
private ArrayList<JButton> classiciconlist = new ArrayList<JButton>(); 

public void addComponentsToPane2(final Container pane) { 

for(int i = 0; i <= 50; i++) { 

    classiciconlist.get(i).addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      if (x == 0) { 
       Preis = Preis - classicpreis;  
       Locale currentlocale = Locale.GERMANY; 
       NumberFormat numberFormatter = 
       NumberFormat.getCurrencyInstance(currentlocale); 
       String classicpreisx = numberFormatter.format(classicpreis); 
       String preisx = numberFormatter.format(Preis); 
       labelsumme.setText(String.valueOf("Summe: " + preisx)); 
       classiciconlist.get(1).setVisible(false); 
      x++; 
      }   

      else if (x == 1) { 
       Preis = Preis - classicpreis;  
       Locale currentlocale = Locale.GERMANY; 
       NumberFormat numberFormatter = 
       NumberFormat.getCurrencyInstance(currentlocale); 
       String classicpreisx = numberFormatter.format(classicpreis); 
       String preisx = numberFormatter.format(Preis); 
       labelsumme.setText(String.valueOf("Summe: " + preisx)); 
       classiciconlist.get(2).setVisible(false); 
       x++; 
      }   
      else if (x == 2) { 
       Preis = Preis - classicpreis;  
       Locale currentlocale = Locale.GERMANY; 
       NumberFormat numberFormatter = 
       NumberFormat.getCurrencyInstance(currentlocale); 
       String classicpreisx = numberFormatter.format(classicpreis); 
       String preisx = numberFormatter.format(Preis); 
       labelsumme.setText(String.valueOf("Summe: " + preisx)); 
       classiciconlist.get(3).setVisible(false); 
       x++; 
      }  


     }}); 


    } 


} 
}  
+0

是否有這三個條件有什麼區別?任何三條條款都完全相同的條款完全可以脫離條件。 – David

+4

這可能更適合codereview.stackexchange.com,雖然我會檢查的第一件事是那些if-elseif塊。它們似乎是相同的,只是使用了列表索引並且可以計算爲'x + 1'(您需要對x進行範圍檢查,例如0 <= x Thomas

+2

重複太多。需要乾燥。只有差異是x值和列表中的索引。 – duffymo

回答

3

你重複你的代碼 - 讓只有1方法,並調用它myMethod(x);

public void myMethod(int i) { 
Preis = Preis - classicpreis;  
Locale currentlocale = Locale.GERMANY; 

NumberFormat numberFormatter = 
NumberFormat.getCurrencyInstance(currentlocale); 
String classicpreisx = numberFormatter.format(classicpreis); 
String preisx = numberFormatter.format(Preis); 
labelsumme.setText(String.valueOf("Summe: " + preisx)); 
classiciconlist.get(i + 1).setVisible(false); 
x++; 
} 
+0

好吧,我現在明白了(花了一段時間才明白)。 這個效果非常好,你節省了我很多時間和線條,謝謝。 – Jennifer96

0

很多線在下面的代碼是重複的。嘗試將公共代碼 移出if/else塊,並僅將條件代碼放入相應的if/else 塊中。

  if (x == 0) { 
       Preis = Preis - classicpreis; 
       Locale currentlocale = Locale.GERMANY; 
       NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale); 
       String classicpreisx = numberFormatter.format(classicpreis); 
       String preisx = numberFormatter.format(Preis); 
       labelsumme.setText(String.valueOf("Summe: " + preisx)); 
       classiciconlist.get(1).setVisible(false); 
       x++; 
      } 

      else if (x == 1) { 
       Preis = Preis - classicpreis; 
       Locale currentlocale = Locale.GERMANY; 
       NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale); 
       String classicpreisx = numberFormatter.format(classicpreis); 
       String preisx = numberFormatter.format(Preis); 
       labelsumme.setText(String.valueOf("Summe: " + preisx)); 
       classiciconlist.get(2).setVisible(false); 
       x++; 
      } else if (x == 2) { 
       Preis = Preis - classicpreis; 
       Locale currentlocale = Locale.GERMANY; 
       NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale); 
       String classicpreisx = numberFormatter.format(classicpreis); 
       String preisx = numberFormatter.format(Preis); 
       labelsumme.setText(String.valueOf("Summe: " + preisx)); 
       classiciconlist.get(3).setVisible(false); 
       x++; 
      } 

此代碼應該是不錯的

 public void actionPerformed(ActionEvent e) { 
      Preis = Preis - classicpreis; 
      Locale currentlocale = Locale.GERMANY; 
      NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale); 
      String classicpreisx = numberFormatter.format(classicpreis); 
      String preisx = numberFormatter.format(Preis); 
      labelsumme.setText(String.valueOf("Summe: " + preisx)); 
      classiciconlist.get(i+1).setVisible(false); 
      x++; 
     } 
1

如果我沒有記錯的話,對各分支之間的唯一區別,如果是classiciconlist.get(X + 1)。如果是這樣,這相當於:

  if (x >= 0 && x <= 2) { 
       Preis = Preis - classicpreis; 
       Locale currentlocale = Locale.GERMANY; 
       NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale); 
       String classicpreisx = numberFormatter1.format(classicpreis); 
       String preisx = numberFormatter.format(Preis); 
       labelsumme.setText(String.valueOf("Summe: " + preisx)); 
       classiciconlist.get(x+1).setVisible(false); 
       x++; 
      } 
0

那麼你有一個巨大的if-else-if_else可以真正縮短爲:

if (x == 0) { 
    classiciconlist.get(1).setVisible(false); 
} else if (x == 1) { 
    classiciconlist.get(2).setVisible(false); 
} else if (x == 2) { 
    classiciconlist.get(3).setVisible(false); 
} 

一切是相同的,但與其他的名字來...

看:

//Preis is calcualted with the same formula no matter the value of x 
Preis -= classicpreis; 
//Locale uis allways German 
Locale currentlocale1 = Locale.GERMANY; 
//Number is the same(all for German Locale) 
NumberFormat numberFormatter1 = NumberFormat.getCurrencyInstance(currentlocale1); 
//same here 
String classicpreisx1 = numberFormatter1.format(classicpreis); 
//und here 
String preisx1 = numberFormatter1.format(Preis); 
//und here 
labelsumme.setText(String.valueOf("Summe: " + preisx1)); 
//x variable is getting incremented no matther what.... 
x++; 
+1

到目前爲止,您的提示爲我工作。仍然重複我猜,但它縮短了我的代碼感謝:)同時我會嘗試弄清楚其他答案可能如何解決。 – Jennifer96

+0

錯過的技巧是:*** classiciconlist.get(x + 1).setVisible(false); ***但您需要小心x值... –