2017-08-28 51 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值... –