2014-05-04 2 views
-1

J'ai essayé de faire une application iPhone qui affiche deux nombres aléatoires puis vous entrez une réponse, puis appuyez sur Soumettre et il devrait afficher une étiquette qui dit soit Droite ou mauvais mais quand je l'ai écrit le code, il affiche toujours droit Voici le code:Je veux faire un contrôle de vue qui est vraiment un test de table de temps

- (IBAction)but01Pressed:(id)sender{ 

    NSLog(@"B111111"); 

    RandomNum = arc4random() %25; 

    if (RandomNum == 0) { 
     RandomNum = arc4random() %25; 
     if (RandomNum == 0) { 
      RandomNum = arc4random() %25; 
     } 
    } 

    //@"GLOBAL=%@ %@",appd.globalString, name.text 

    lab01.textColor = [UIColor blueColor]; 
    lab02.textColor = [UIColor blackColor]; 

    textF01.hidden = NO; 
    textF02.hidden = NO; 
    textF03.hidden = NO; 
    adotime01.hidden = NO; 
    equal01.hidden = NO; 
    SubmitFQ01.hidden = NO; 



    textF01.text = [NSString stringWithFormat:@"%d",RandomNum]; 

    RandomNum = arc4random() %15; 

    if (RandomNum == 0) { 
     RandomNum = arc4random() %15; 
     if (RandomNum == 0) { 
      RandomNum = arc4random() %15; 
     } 
    } 

    textF02.text = [NSString stringWithFormat:@"%d",RandomNum]; 



} 

`enter code here`- (IBAction)submitbutPressed:(id)sender; 
{ 
    if (textF03.text == [NSString stringWithFormat:@"%d",[textF01.text intValue] * [textF02.text intValue]]) { 
     label066.text = @"Wrong"; 
     label066.hidden = NO; 
    } 
else 
    { 
     label066.text = @"Right"; 
     label066.hidden = NO; 
    } 

     } 
+0

Il semble que vous ayez 'RIGHT' et' WRONG' associé au cas incorrect dans la dernière instruction if ... – darthbith

Répondre

0

Ce:

if (textF03.text == [NSString stringWithFormat:@"%d",[textF01.text intValue] * [textF02.text intValue]]) { 

doit être:

if ([textF03.text isEqualToString:[NSString stringWithFormat:@"%d",[textF01.text intValue] * [textF02.text intValue]]]) { 

ou mieux:

if ([textF03.text intValue] == [textF01.text intValue] * [textF02.text intValue]) { 

Vous ne pouvez pas utiliser == pour comparer deux valeurs d'objet pour voir si les objets représentent la même valeur. Et comme mentionné dans les commentaires, lorsque l'instruction if est vraie, il s'agit de la réponse «droite» et non de la réponse «Incorrect».

Side note:

Pourquoi ce code:

RandomNum = arc4random() %25; 

if (RandomNum == 0) { 
    RandomNum = arc4random() %25; 
    if (RandomNum == 0) { 
     RandomNum = arc4random() %25; 
    } 
} 

Je devine que vous voulez vraiment un nombre aléatoire dans la plage 1-24, correct? Si oui, il suffit de faire ceci:

randomNum = arc4random() % 24 + 1; // gives 1-24 

Mieux encore, utilisez:

randomNum = arc4random_uniform(24) + 1; 

Faire un changement similaire pour votre autre valeur.

+0

Il vous manque et terminez paren sur '[textF03.text intValue'. – Undo

Questions connexes