2013-03-01 34 views
1

我有一個問題,我嘗試了幾種方法,但找不到解決方案。以中間值顯示警報(Objective-C或任何其他)

問題(例如在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 
[...] 

任何想法?

謝謝!

回答

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]; 
    } 
} 
+0

你有'roundToMultipleOf10(n - 3)'。 'n'應該是'價值'嗎? – rmaddy 2013-03-01 02:00:48

+0

固定。謝謝。 – 2013-03-01 02:02:39

2

你的要求意味着其中x是一些10S數字是ALERT!!只出現X0,X1和X2:做你的答案混合泳之後

for (int i = 0; i < 1000; i += 3) { 
    if (i > 10 && i % 10 <= 2) { 
     NSLog(@"ALERT!!"); 
    } 
} 
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; 
    }