数到80,跳过100个数字范围内的每第5个数字

时间:2014-07-10 12:13:32

标签: php actionscript range skip ceil

我正在进行一项有80个编号问题的测验。在每4个问题之后,将会有无数的第5个奖金。这100个问题在数据库表中具有索引号(1-100)。也许为奖金问题使用单独的表更合乎逻辑,但客户希望按顺序在一个表中提问。

我不想在测验的界面中显示奖励问题索引号码,因此我需要跳过100个问题范围内的每第5个索引号码。

我确实做了一个简单的计算,当我在一个循环中检查它时效果很好,但不知怎的,我觉得这是一个相当肮脏的解决方案(ceil)。有没有办法以更整洁或更合乎逻辑的方式编写这些代码?

的ActionScript:

for (var i:Number = 1; i < 101; i++) {
    var displayIndex:Number = Math.ceil((i/5) * 4);
    trace("i:" + i + " / " + displayIndex);
}

PHP:

for ($i = 1; $i < 101; $i++) {
    echo "i: " . $i . " / displayIndex: " . (ceil(($i/5) * 4) . "<br>");
}

修改 让我试着想象一下这个

index-number =  1 -> display-number =  1
index-number =  2 -> display-number =  2
index-number =  3 -> display-number =  3
index-number =  4 -> display-number =  4
index-number =  5 -> display-number =  none
index-number =  6 -> display-number =  5
index-number =  7 -> display-number =  6
index-number =  8 -> display-number =  7
index-number =  9 -> display-number =  8
index-number = 10 -> display-number =  none
etc...

2 个答案:

答案 0 :(得分:1)

if($i % 5 != 0) {
  // do your stuff
}
else {
  // skipped
}

根据预期产出: -

for ($i = 1; $i < 101; $i++) {
    echo 'index-number = '. $i.' -> display-number ='. (($i % 5 != 0) ? $i : 'none').'<br>';  
}

输出: -

index-number = 1 -> display-number =1
index-number = 2 -> display-number =2
index-number = 3 -> display-number =3
index-number = 4 -> display-number =4
index-number = 5 -> display-number =none
index-number = 6 -> display-number =6
index-number = 7 -> display-number =7
index-number = 8 -> display-number =8
index-number = 9 -> display-number =9
index-number = 10 -> display-number =none
and so on....

答案 1 :(得分:1)

你的任务充满异国情调,你的决定可以公平地完成它。它已经足够简化了,但除了你之外没有人会理解它。

我建议只是添加评论。然而,还有很多其他的方法,我不能说它们更优雅,但也许更加不言自明。

$counter = 1;
for ($i = 1; $i < 21; $i++) {
    $displayNum = $counter;
    if ($i % 5 == 0) {
        $counter--;
        $displayNum = "none";
    }
    echo "index-number = $i -> display-number = $displayNum <br/>";
    $counter++;
}

将导致:

index-number = 1 -> display-number = 1 
index-number = 2 -> display-number = 2 
index-number = 3 -> display-number = 3 
index-number = 4 -> display-number = 4 
index-number = 5 -> display-number = none 
index-number = 6 -> display-number = 5 
index-number = 7 -> display-number = 6 
index-number = 8 -> display-number = 7 
index-number = 9 -> display-number = 8 
index-number = 10 -> display-number = none 
index-number = 11 -> display-number = 9 
index-number = 12 -> display-number = 10 
index-number = 13 -> display-number = 11 
index-number = 14 -> display-number = 12 
index-number = 15 -> display-number = none 
index-number = 16 -> display-number = 13 
index-number = 17 -> display-number = 14 
index-number = 18 -> display-number = 15 
index-number = 19 -> display-number = 16 
index-number = 20 -> display-number = none 

如果你试图用三元运算符来缩小它,你最终会得到与你的代码一样的代码,这是代码,而不是解释。

您可以跳过continue的迭代并在此之前添加所需的输出。同样的结果可以通过以下方式实现:

$counter = 1;
for ($i = 1; $i < 21; $i++) {
    if ($i % 5 == 0) {
        echo "index-number = $i -> display-number = none <br/>";
        continue; // more explanatory for SKIP THIS ITERATION
    }
    echo "index-number = $i -> display-number = $counter <br/>";
    $counter++;
}

这样你就会打印none并跳过迭代的执行,因此$counter不会递增,而在第6次迭代时它将是5。