在Java代码中更好地使用for循环
我的代码也按照我想要的方式工作,但它使用了很多行 这是关于食品订购终端的,我给你们看的部分是关于删除的 购物车上的按钮 我不想编辑Arraylist中的每一个JButton。我以为一个循环可以帮上忙,但我不知道怎么做 格雷廷斯在Java代码中更好地使用for循环,java,Java,我的代码也按照我想要的方式工作,但它使用了很多行 这是关于食品订购终端的,我给你们看的部分是关于删除的 购物车上的按钮 我不想编辑Arraylist中的每一个JButton。我以为一个循环可以帮上忙,但我不知道怎么做 格雷廷斯 public class Bestellterminal { private int x = 0; private double Preis = 0; private double classicpreis = 2.5; private ArrayList<J
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++;
}
}});
}
}
}
您正在重复您的代码-只创建1个方法并将其命名为myMethodx
您正在重复您的代码-只创建1个方法并将其命名为myMethodx 下面代码中的许多行是重复的。尝试移动公共代码 跳出if/else块,只在相应的if/else中放入条件代码 街区 这个代码应该是好的
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++;
}
下面代码中的许多行是重复的。尝试移动公共代码
跳出if/else块,只在相应的if/else中放入条件代码
街区
这个代码应该是好的
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++;
}
如果我没有弄错的话,If的每个分支之间唯一的区别就是ClassicOnlist.getx+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++;
}
如果我没有弄错的话,If的每个分支之间唯一的区别就是ClassicOnlist.getx+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++;
}
好吧,你有一个巨大的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);
}
其他一切都是一样的,但变量的名称不同
看:
好吧,你有一个巨大的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);
}
其他一切都是一样的,但变量的名称不同
看:
这三种情况有什么区别吗?这三种情况中存在相同的任何一行都可以完全从条件中删除。这可能更适合codereview.stackexchange.com,尽管我要检查的第一件事是如果elseif阻塞。它们似乎是相同的,除了所使用的列表索引外,一个可以计算为x+1。然后,您需要对x进行范围检查,例如,0太多重复。需要干燥。唯一的区别是x值和列表中的索引。您不应该使用固定值,我请解释代码的用途。x应该是什么?你期望得到什么结果?这三个条件之间有什么区别吗?这三种情况中存在相同的任何一行都可以完全从条件中删除。这可能更适合codereview.stackexchange.com,尽管我要检查的第一件事是如果elseif阻塞。它们似乎是相同的,除了所使用的列表索引外,一个可以计算为x+1。然后,您需要对x进行范围检查,例如,0太多重复。需要干燥。唯一的区别是x值和列表中的索引。您不应该使用固定值,我请解释代码的用途。x应该是什么?你期待什么结果?到目前为止你的建议对我有效。我猜还是重复,但它缩短了我的代码。谢谢:与此同时,我将尝试找出其他答案的解决方法。缺少的技巧是:ClassicOnlist.getx+1.setVisiblefalse;但是你需要小心x值…到目前为止你的提示对我有用。我猜还是重复,但它缩短了我的代码。谢谢:与此同时,我将尝试找出其他答案的解决方法。缺少的技巧是:ClassicOnlist.getx+1.setVisiblefalse;但是你需要小心x值…好的,我现在明白了,需要一段时间才能理解。这很好用,你帮我节省了很多时间和台词,谢谢。好的,我现在明白了,我花了一段时间才明白。这很好用,你帮我省了很多时间和台词,谢谢。