【发布时间】:2011-07-28 09:34:57
【问题描述】:
每当我需要在我的 for 循环中每 300 次迭代执行某个操作时,我都会尝试显示(有意义吗?)
这是我想要做的代码,但不是我想要的方式:
for I := 0 to 2000 do
Begin
if I = 300 then
DoAnAction;
if I = 600 then
DoAnAction
if I = 900 then
DoAnAction
if I = 1200 ......... Same action all over, but I don't want to check all those conditions!
End;
所以有人告诉我使用mod 运算符,我就是这样做的:
for I := 0 to 2000 do
Begin
if I mod 300 = 299 then
DoAnAction;
End;
然而,使用上述sn-p的结果会在299、599、899 ....
如何使用 Mod 运算符使其在 300、600、900 ...... (并且做if I mod 300 = 300 没有用)
谢谢!
【问题讨论】:
-
0 mod 300 = 300 mod 300 = 0。i mod n 在 [0..n-1] 范围内。
-
哇,这么多答案!好吧,我想第一个得到了标记..
-
打勾最好的,而不是最快的
-
@David - 你推荐哪一个?
-
@David - 我在这里激怒了很多人,所以我先问专家。
标签: delphi math integer modulo