2016-01-09 133 views
-1

我正在嘗試清理我的代碼,以使其在'部分'下運行。 有一個叫intro(),另外一個叫second()等..代碼行被忽略

問題是當我代碼intro())第一部分切換下一個一個(second())的應該先運行清潔代碼,但不是真的!

code基本上在list應該看起來像一個聊天寫入文本,所以intro()是文本的第一部分,那麼就應該清除聊天並開始編寫內部second()其他文本。

這是我的代碼,我們來看一看:

int counter; 
boolean introDone = false; 



//Intro 
public void intro(){ 
     write(answers[counter], buttonText[counter]); 
} 

public void second(){ 
      write("So these are the rules:", R.string.go); 
} 

public void hello(View view){ 
    Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
    toast.show(); 
    if(counter <= 2){ 
    intro(); 
    }else if(counter == 3){ 
     clear(); 
    }else if (counter > 2 && counter < 5) { 
    second();} 

    counter++; 
} 

}

這奏效了:

//Intro 
public void intro(){ 
     write(answers[counter], buttonText[counter]); 

} 

public void second(){ 
      write("So these are the rules:", R.string.go); 
} 

public void hello(View view) { 
    Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
    toast.show(); 
    if (counter <= 2) { 
     intro(); 
    } else if(counter == 3) { 
     introDone = true; 
    } 
    if (introDone) { 
     clear(); 
     introDone = false; 
    } 
    if (counter > 2 && counter < 5) { 
      second(); 
     } 

    counter++; 
} 

}

+0

嘗試if(introDone == true)..如果這樣不起作用,那麼ti意味着您的IntroDone爲false。 – Kristo

+3

@ Kristo1990如何將if(introDone)改爲if(introDone == true)會改善什麼?這兩個代碼將以相同的方式工作,並且您的提議僅引入了像if(introDone = true)那樣的錯字錯誤的可能性,這就是爲什麼應該避免使用'== true'的原因。 – Pshemo

+1

這似乎並沒有改變@ Kristo1990 – FET

回答

0

我要去假設布爾型introDone變量永遠不會是真的。在你的intro()方法中,你用if語句包裝它來檢查計數器是否等於或小於2。這是很難說沒有完整的源代碼,但我猜想,如果你改變了你的代碼,以這樣的:

//Intro 
public void intro(){ 

    //if (counter <= 2) { 
     // write(answers[counter], buttonText[counter]); 
    //}else{ 
     introDone = true; 
    //} 

} 

public void second(){ 
    if(introDone) { 
     listViewMother.setAdapter(null); //THIS SHOULD CLEAN THE LIST, BUT INSTEAD WHEN I RUN THE CODE IT LOOKS LIKE IT JUST IGNORES IT 
     if (counter > 2 && counter < 5) { 
      write("So these are the rules:", R.string.go); 
     } 
    } 
} 

public void hello(View view){ 
    Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
    toast.show(); 
    intro(); 
    second(); 
    counter++; 
} 

一切都將正常工作。

從那裏你可以看看你如何增加計數器變量的邏輯,看看是否是這個問題。

此示例將運行call intro()和second()10次。

public void hello(View view){ 
     Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
     toast.show(); 
     for (int counter = 0; counter < 10; counter++) { 
      intro(); 
      second(); 
     } 

} 
+0

嗨,詹姆斯,我認爲你是對的,你澄清了我的if/else語句的概念,所以謝謝。順便說一句,我仍然不能真正說出這是否工作,因爲我需要一些文本之前應該清理,但現在我正在找出實現t的方法他最後的結果:) – FET

+0

好,請標記這是正確的答案,如果它的工作。 –

+0

您可以使用某種while或if語句通過調用intro()和second()方法來循環,每個循環都會增加計數器變量。這將解決您的問題。 –

0

如何變計數器和可變introDone初始化?

我假設變量計數器在節目的開始具有值0,並且可變introDone是假的。如果是這樣的話,則代碼第二個部分不執行。例如:

public void hello(View view){ 
    // --> here, counter=0 and introDone=false 
    Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
    toast.show(); 
    intro(); // --> counter is still 0, so the variable introDone is still false 
    second(); // --> because introDone=false then the code is not executed 
    counter++; 
} 
+0

嘗試查看更新的問題,我已經添加了初始的東西,哦,並通過它的工作方式在第一次點擊 – FET