1

我有一个问题,我尝试了几种方法,但我找不到解决方案。

问题(例如在 Objective-C 中) 我有一个从 0 开始的进度,并且对于每次迭代,该值增加 3 个点。即 0、3、6、9、12、15 等

好吧,我需要当计数器超过10时,显示警报,但只有当超过10、20、30、40等时,中间值(3、6、9等)不应该显示.

例如:

0 -> nothing
3 -> nothing
6 -> nothing
9 -> nothing
12 -> ALERT!!
15 -> nothing
18 -> nothing
21 -> ALERT!!
24 -> nothing
27 -> nothing
30 -> ALERT!!
33 -> nothing
36 -> nothing
[...]

任何想法?

谢谢!!

4

3 回答 3

2

value向下舍入为 10 的倍数。向下value-3舍入为 10 的倍数。如果四舍五入的值不同,则显示警报。

static int roundToMultipleOf10(int n) {
    return 10 * (n / 10);
}

static void showAlertIfAppropriateForValue(int value) {
    if (roundToMultipleOf10(value) != roundToMultipleOf10(value - 3)) {
        UIAlertView *alert = [[UIAlertView alloc] init...];
        [alert show];
    }
}
于 2013-03-01T01:58:37.133 回答
2

您的要求意味着ALERT!!仅出现在 x0、x1 或 x2 中,其中 x 是 10 位数字:

for (int i = 0; i < 1000; i += 3) {
    if (i > 10 && i % 10 <= 2) {
        NSLog(@"ALERT!!");
    }
}
于 2013-03-01T01:58:50.113 回答
0

混合所有答案后,我的最终解决方案。谢谢!

    // Round function
    int (^roundIntegerTo10)(int) =
    ^(int value) {
        return value / 10 * 10;
    };

    // Progress evaluator
    if (_currentProgress > _nextProgress && _currentProgress >= 10) {
        NSLog(@"ALERT!!!");
        _nextProgress = roundIntegerTo10(_currentProgress) + 10;
    }
于 2013-03-01T14:40:18.113 回答