我正在做一個簡單的應用程序,其中用戶單擊一個按鈕,將TextView更改爲相應的字符串,但是當我的第一個if語句被滿足時,它不會繼續執行以下if語句應該是。Java if語句被忽略
if (index == 0 && index > -1 && index < 5) {
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
index++;
text1.setText("1");
}
});
這是我的第一個if語句,它設置TextView的「1」,然後應該添加到整數「指數」,這原來該指數的值設置爲「1」,這應該結束這種說法,因爲它不再有資格,如果聲明將開始以下內容。
if (index == 1 && index > -1 && index < 5) {
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text2.setText("1");
index++;
}
});
因爲以前if
陳述,該指數的變量設置爲「1」,這if
說法應該開始和以前結束的現在,但這種情況並非如此,即使變量不再有資格,這不是」不要停下來,下一個if語句不會開始。就好像if
語句被忽略。
UPDATE。
我固定我的問題,這是我改變了代碼:
one.setOnClickListener(new View.OnClickListener(){public void onClick(View v){
if(index == 0){
text1.setText("1");
index++;
}else if(index == 1){
text2.setText("1");
index++;
}else if(index == 2){
text3.setText("1");
index++;
}else if(index == 3){
text4.setText("1");
index++;
}
}});
'if(index == 0 && index> -1 && index <5)'等於'if(index == 0)' –
基本代碼流的問題? ...你的if語句只被檢查一次(在onCreate中的prolly)而不是在每個按鈕點擊... – Selvin
'if(index == 0 && index> -1 && index <5)'沒有什麼意義。 – rtheunissen