[英]can someone help me with this code for school?
This is my code: 这是我的代码:
for (Article a : articles) {
boxNumber = magazine.getFfd().searchArticleInBox(a);
if (boxNumber >= 3) {
text = text + "Place box " + boxNumber + "\n";
}
}
textArea.append(text);
It does work i get: 它确实可以工作:
Place box 3
Place box 3
Place box 3
Place box 3
Place box 4
Place box 4
This is not how I want it. 这不是我想要的。 I hope someone can tell me how I get it only one time when the number changes, so I get:
我希望有人能告诉我,当数字改变时,我一次只能得到它,所以我得到:
Place box 3
Place box 4
Keep the last value and check against it: 保留最后一个值并对照它:
int last = 0;
for (Artikel a : artikelen) {
doosnummer = magazijn.getFfd().zoekArtikelInDoos(a);
if (doosnummer >= 3) {
if (last != doosnummer) {
last = doosnummer;
text = text + "Plaats doos " + doosnummer + "\n";
}
}
}
textArea.append(text);
Note that it would be more efficient to append the String
s to a single StringBuilder
object instead of using String
concatenation, which keeps creating new String
objects: 请注意,将
String
追加到单个StringBuilder
对象而不是使用String
串联会更有效,因为这会继续创建新的String
对象:
StringBuilder text = new StringBuilder();
int last = 0;
for (Artikel a : artikelen) {
doosnummer = magazijn.getFfd().zoekArtikelInDoos(a);
if (doosnummer >= 3) {
if (last != doosnummer) {
last = doosnummer;
text.append("Plaats doos ");
text.append(doosnummer);
text.append("\n");
}
}
}
textArea.append(text.toString());
You have to remember the old number and only print if it changed: 您必须记住旧数字,并且只有在更改后才打印:
int oldDoosnummer =0;
for (Artikel a : artikelen){
doosnummer = magazijn.getFfd().zoekArtikelInDoos(a);
if (doosnummer >= 3 && doosnummer != oldDoosnummer){
text = text + "Plaats doos " + doosnummer + "\n";
oldDoosnummer = doosnummer;
}
} textArea.append(text);
Try this: 尝试这个:
ArrayList<Integer> oldOccurrenceArrayList = new ArrayList<>();
for (Artikel a : artikelen) {
doosnummer = magazijn.getFfd().zoekArtikelInDoos(a);
if (doosnummer >= 3 && !oldOccurrenceArrayList.contains(doosnummer))
{
text = text + "Plaats doos " + doosnummer + "\n";
oldOccurrenceArrayList.add(doosnummer);
}
}
textArea.append(text);
You create an ArrayList
with all the values that you already passed. 您使用所有已传递的值创建一个
ArrayList
。 This way you never get any double values. 这样,您永远不会获得任何双精度值。
Try this 尝试这个
for (Artikel a : artikelen) {
doosnummer = magazijn.getFfd().zoekArtikelInDoos(a);
String check = "Plaats doos " + doosnummer;
if (doosnummer >= 3 && text.contains(check)==false) {
text = text + check + "\n";
}
}
textArea.append(text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.