-2

ゲームでは...「完了」ボタンを押すと、2つの「ifステートメント」があります。1つは次のレベルに移動し、もう1つは家に戻ります。一度に1つだけが発生するようにするにはどうすればよいですか?現在の動作方法は、ユーザーが正しいか間違っているかに関係なく、勝ち負けの「アラートビュー」ポップアップの両方です。これはそれがどのように見えるかです:

-(IBAction)done {

    if ([entry.text isEqualToString:sentance.text]) {

        UIAlertView *alert = [[UIAlertView alloc] 
                             initWithTitle:@"Cleared!" 
                             message:@""
                             delegate:nil 
                             cancelButtonTitle:@"Ok" 
                             otherButtonTitles: nil];
        [alert show];
        [alert release];

        seconds.text = @"5 Seconds";
    }

    if ([entry.text isEqualToString:entry.text]) {

        NSString *nssScore= [NSString stringWithFormat:@"Final Score: \n %i Points", Score];

        UIAlertView *alert = [[UIAlertView alloc]
                             initWithTitle:@"Nope! Wrong"
                             message:nssScore
                             delegate:nil
                             cancelButtonTitle:@"Ok"
                             otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}
4

3 に答える 3

3

あなたの2番目に何か問題がありますif

if ([entry.text isEqualToString:entry.text]) {

これは常に真である場合、なぜentry.textそれ自体と等しくてはならないのでしょうか?

また、2つのifステートメントがあり、1つだけが真である場合は、常にelse if2番目のステートメントにaを使用します。

if (<condition>) {

} else if (<other condition>) {

}

trueの場合condition、trueであっても、もう一方ifは実行されませんother condition。しかし、あなたの場合、あなたの2番目ifは私にはナンセンスに思えます。常に真実であるというifことは役に立たず、完全に除外されます。コードはと同等です

if (<condition>) {

} else {

}
于 2013-01-28T14:19:01.800 に答える
0

elseif条件を使用できます。

このような :

-(IBAction)done {

    if ([entry.text isEqualToString:sentance.text]) {

        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"Cleared!" 
                              message:@""
                              delegate:nil 
                              cancelButtonTitle:@"Ok" 
                              otherButtonTitles: nil];
        [alert show];
        [alert release];

        seconds.text = @"5 Seconds";
    }
    else if ([entry.text isEqualToString:entry.text]) {

        NSString *nssScore= [NSString stringWithFormat:@"Final Score: \n %i Points", Score];

        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Nope! Wrong"
                              message:nssScore
                              delegate:nil
                              cancelButtonTitle:@"Ok"
                              otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

または、各ブロック「return;」にコード行を追加できます。

このような:

于 2013-01-28T14:20:37.460 に答える
-1

私は答えを得ました...

    if ([a.text isEqualToString:b.text] == FALSE) {

condition

}

ありがとう!

于 2013-01-28T22:46:24.213 に答える