2011-09-17 54 views
0

我有一個for循環的問題。 這是我的代碼android和for循環

int i; 
int from= Integer.valueOf(field_from.getText().toString()); 
int to = Integer.valueOf(field_to.getText().toString()); 
Log.d("wec", "from->"+from+" to->"+to); 
for(i=from; from < to; i++){ 
    Log.d("wec", "i->"+i); 
} 

field_from的值爲1,field_to的值是10 當我運行該腳本的應用程序是瘋了,因爲從100開始一個循環無限

誰知道爲什麼?

回答

1

的比較邏輯是錯誤的for循環

,而不是(從<到)
你應該有(我<於)在for循環

1
for(i=from; i < to; i++){ 
    Log.d("wec", "i->"+i); 

使用本。

3
for(i=from; from < to; i++) 

您正在遍歷條件from < to。這兩個變量都不會在循環中發生變化。

0

for循環中的錯誤條件應爲i < to而不是from < to