我在尝试将分数显示为高分时遇到问题。
这是我的代码,用于以正确的格式在标签上显示时间。
- (void)populateLabelwithTime:(int)milliseconds {
int seconds = milliseconds / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
seconds -= minutes * 60;
minutes -= hours * 60;
resultScoreLabel.text = [NSString stringWithFormat:@"%ds:%dms",seconds, milliseconds%1000];
if (currentTime > highScore) {
[[NSUserDefaults standardUserDefaults] setInteger:currentTime forKey:@"highscore"];
resultHighScoreLabel.text = [NSString stringWithFormat:@"%i",highScore +10];
}
else {
}
}
-(void)scoring {
currentTime += 10;
[self populateLabelwithTime:currentTime];
highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"highscore"];
}
乐谱格式正确。 得分为:1s:186ms
但是高分出现错误 您的最高分是:1186
知道这一行是错的
resultHighScoreLabel.text = [NSString stringWithFormat:@"%i",highScore +10];
但我不知道如何让它工作
有什么建议吗??
最佳答案
这是快速修复:
int newHighscore = highScore+10;
int seconds = ((int)floor((newHighscore)/1000));
int milliseconds = newHighscore%1000;
resultHighScoreLabel.text = [NSString stringWithFormat:@"%is:%ims",seconds,milliseconds];
但是我强烈建议改变你处理高分的方式
关于iOS 在 UILabel 上将时间显示为分数和高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23971397/