如何阻止分数不低于零?

时间:2014-03-23 22:45:25

标签: ios iphone objective-c

我正在制作文字游戏,但我无法弄清楚如何在我创建的基本评分中将零点归零。用户不得超过他或她的分数减号,因此最低分数只能为零...

- (IBAction)btncheck:(id)sender {
NSString *answer = [_textbox.text stringByReplacingOccurrencesOfString:@" " withString:@""]
if([answer isEqualToString:@""]){
}
else  
if ([answer isEqualToString:@"q"]) {
    // String is correct, resign keyboard
    _keyboard.hidden = YES;
    _textXclear.hidden = YES;
    //Perfect button
    [_closeone setHidden:NO];
    [_wrongone setHidden:YES];
    score = score +100;
    [scoreLabel setText:[NSString stringWithFormat:@"score: %d", score]];
   coins = coins +5;
    [coinsLabel setText:[NSString stringWithFormat:@"%d", coins]];
}
else {
    [_wrongone setHidden:NO];
    score = score -5 ;
    [scoreLabel setText:[NSString stringWithFormat:@"score: %d", score]];
   closeonechange.text = @"Correct!";

}

哦,如果分数变为零,用户只能获得1枚硬币。

我将如何做到这一点?

3 个答案:

答案 0 :(得分:4)

只需使用MAX

即可
score = MAX(0, score - 5);

答案 1 :(得分:0)

你在找这个吗?

score = MAX(0, score - 5);
if (score == 0) coins += 1;

答案 2 :(得分:0)

score = ((score-5) < 0) ? 0 : (score-5);