0

これはおそらく最も簡単で最も簡単な質問です。

そのため、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 //
4

3 に答える 3

3

あなたのiは整数です、それは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.
 }
于 2011-06-01T15:58:06.917 に答える
0

私はintです。したがって0+0.25 = 0

于 2011-06-01T15:58:31.540 に答える
0

floatの代わりに使用してintください。

式の値i+0.25 =>(0 + 0.25)=>0.25であるためです。

i = i+0.25;

ここで、値0.25を整数に割り当てているため、毎回0になり、条件inwhile が0でfalseになることはないため、無限ループになります。

したがって、コードは

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.
 }
于 2011-06-01T15:58:38.167 に答える