2011-06-01 64 views
0

這可能是最簡單/最遲的問題。viewDidLoad變成無限循環。幫助

所以我想在viewDidLoad方法與值初始化數組的0.25 0至3的增量,並且在這裏我可以看到一個無限循環。

NSArray *pickArray3 = [[NSMutableArray alloc] init]; 
int i = 0; 
//for(i = 0.25; i<=3; i=i+0.25) 
while (i<3) 
{ 
//NSString *myString = [NSString stringWithFormat:@"%d", i]; 
    i=i+0.25; 
    NSLog(@"The Value of i is %d", i); 
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray. 
} 
NSLog(@"I am out of the loop now"); 
self.doseAmount=pickArray3; 
[pickArray3 release]; 

,這是輸出。

2011-06-01 11:49:30.089 Tab[9837:207] The Value of i is 0 
    2011-06-01 11:49:30.090 Tab[9837:207] The Value of i is 0 
    2011-06-01 11:49:30.091 Tab[9837:207] The Value of i is 0 
    2011-06-01 11:49:30.092 Tab[9837:207] The Value of i is 0 
    // And this goes on // 
    // I am out of the loop now does not get printed // 

回答

3

你是我的整數,它永遠不會增加0.25。使用浮點數或雙精度。

**float** i = 0; 
//for(i = 0.25; i<=3; i=i+0.25) 
while (i<3) 
{ 
//NSString *myString = [NSString stringWithFormat:@"**%f**", i]; 
    i=i+0.25; 
    NSLog(@"The Value of i is **%f**", i); 
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray. 
} 
+0

..................... FML – Legolas 2011-06-01 15:59:28

+0

非常細心的你tofutim .... – Sid 2011-06-01 16:08:09

+1

變化的NSLog ... – Jhaliya 2011-06-01 16:11:59

0

我是一個int。因此0+0.25 = 0

+0

.................. FML – Legolas 2011-06-01 15:59:09

+0

這實在是一個評論,而不是一個問題的答案。請使用「添加評論」爲作者留下反饋。 – 2012-08-11 09:06:49

+0

@AalokParikh我不同意。 – dasdom 2012-08-11 18:44:09

0

使用float代替int

因爲每次表達式值i + 0.25 =>(0 + 0.25)=> 0.25。

i = i+0.25; 

現在你分配值0.25整數因此它變成0 while每次和條件將永遠是假的0,所以它去無限循環

所以,你的代碼必須是

float i = 0; 
//for(i = 0.25; i<=3; i=i+0.25) 
while (i<3) 
{ 
//NSString *myString = [NSString stringWithFormat:@"%d", i]; 
    i=i+0.25; 
    NSLog(@"The Value of i is %f", i); 
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray. 
}