你someMethod()
將計數器復位到3。因此它會總是回報3
。因此您的if
塊永遠不會被執行。
someMethod()
在邏輯上等同於以下,禁止任何併發詭計:
public int someMethod() {
intVar = 3;
return 3;
}
因此您actionPerformed(...)
在邏輯上等同於以下(同上):
public void actionPerformed(ActionEvent e) {
if (someCondition){
clickedButton.setVisible(false);
anotherMethod();
if (3==0){
//Do something
}
}
}
如果計數器應該只是減少,並且當intVar == 0
只是在您的比較中直接使用intVar時應該執行if塊:
public int anotherMethod(){
return --intVar;
}
public void actionPerformed(ActionEvent e) {
if (someCondition){
clickedButton.setVisible(false);
anotherMethod();
if (intVar==0){
//Do something
}
}
}
或者刪除someMethod()
分配:
public int someMethod() {
return intVar;
}
或者,在這裏得到擺脫someMethod()
調用和返回更新後的計數器值 'anotherMethod()' 改變代碼:
public int anotherMethod(){
return intVar--;
}
public void actionPerformed(ActionEvent e) {
if (someCondition){
clickedButton.setVisible(false);
if (anotherMethod()==0){
//Do something
}
}
}
'問題是計數器變量每次都得到重置'爲什麼你的'someMethod()'總是將變量重置爲3? – camickr
'所以intVar真的等於myarraylist.size()。' - 那麼你應該使用myArrayList.size()並去掉變量。您不需要保留重複的信息。正如你所看到的那樣,它會引起問題 – camickr
它作爲一個計數器,但我需要存儲什麼數字它的所以我可以使用if語句我發佈我的實際代碼的pastebin下面 – ugCode