2011-06-01 2 views
0

C'est probablement la question la plus facile/la plus faible.viewDidLoad devient une boucle infinie. AIDE

Donc j'essaie d'initialiser un tableau avec des valeurs de 0 à 3 incréments de 0.25 dans la méthode viewDidLoad, et je peux voir une boucle infinie ici.

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]; 

Et c'est la sortie.

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 // 

Répondre

3

Votre i est un nombre entier, il n'augmentera jamais de 0,25. Utilisez un flotteur ou double.

**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

+0

Très observatrice de vous tofutim .... – Sid

+1

changement NSLog ... – Jhaliya

0

i est un entier. Par conséquent, 0+0.25 = 0.

+0

.................. FML – Legolas

+0

Ceci est vraiment un commentaire, pas une réponse à la question. Veuillez utiliser "ajouter un commentaire" pour laisser un commentaire à l'auteur. –

+0

@AalokParikh Je ne suis pas d'accord. – dasdom

0

utilisez float au lieu de int.

car chaque fois la valeur d'expression i + 0.25 => (0 + 0.25) => 0.25.

i = i+0.25; 

Maintenant, vous assignez la valeur de 0,25 à un nombre entier donc il devient 0 à chaque fois et condition while ne sera jamais faux avec 0, donc il va boucle infinie.

Ainsi, votre code doit être

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. 
}