我有一个方法,当被调用时,使用for循环检查数组为0.如果找到一个,则将其替换为1(暂时)。出于某种原因,在调用方法后,元素似乎重置为0.
public int inv[] = new int[5];
boolean confirmation;
public void requestPickUp() {
confirmation = false;
for (int i = 0; i < 5; i++){
if (inv[i] == 0 && !confirmation) {
confirmation = true;
inv[i] = 1;
}
}
}
public void display() {
for (int i2 = 0; i2 < 5; i2++) {
System.out.println(inv[i2]);
}
System.out.println("-------------------------------------------------");
}
用方法调用方法
inv.requestPickUp();