如果数字条件及其在iOS中的增量为16

时间:2013-12-10 09:29:15

标签: ios objective-c

我有一个叫count的整数,我希望它在1或17或33到永远相同的序列,即count + = 16 第一个球被取消隐藏,同样的事情,当计数到达2或18或34永远相同的顺序,即计数+ = 16,第二个球未被隐藏 起初我输入了这段代码,但是如果条件

,很难用增量键入所有数字。
if(count == 1 || count == 17 || count == 33 || count == 49 || count == 65 || count == 81 || count == 97 || count == 113 || count == 129 || count == 145  || count == 161 || count == 177 || count == 193 || count == 209 || count == 225 || count == 241)
{
    _firstBall.hidden = NO;
}

if(count == 2 || count == 18 || count == 34 || count == 50 || count == 66 || count == 82 || count == 98 || count == 114 || count == 130 || count == 146  || count == 162 || count == 178 || count == 194 || count == 210 || count == 226 || count == 242)
{
    _secondBall.hidden = NO;
}

2 个答案:

答案 0 :(得分:3)

试试这个:

if( ((count-1) % 16) == 0) {
    _first.hidden = NO;
}

if( ((count-2) % 16) == 0) {
    _secondBall.hidden = NO;
}

答案 1 :(得分:0)

%运算符返回整数余数(或模数)。你的第一个序列:1,17,33是16 * n + 1所以剩下的除以16是1.同样你的第二个序列是16 * n + 2.

所以你可以:

switch (count % 16) // switch on the remainder of division by 16
{
   case 1:
      _firstBall.hidden = NO; break;
   case 2:
      _secondBall.hidden = NO; break;
   // add more cases if needed
}