For循环 - 第一次运行时不检查条件?

aps*_*nce 0 language-agnostic for-loop actionscript-3

我想以一定数量启动我的for循环,并使其循环.像这样:

5,6,7,8,9,10,0,1,2,3,4

如果最大数量为10,则从5开始.

原因是因为我想以一定的顺序遍历游戏中的每个牌(从上到下,然后向右).

我尝试过以下但跳过一个数字:

for(var i = position[0]+1; i != position[0]; i++){
    for(var j = position[1]+1; j != position[1]; j++){
        if(j > 11){
            j = 0;
        }
        if(i > 23){
            i = 0;
        }
        if(levelMap[i][j] == type){
            return position;
        }
        trace("j" + j);
    }
    trace(i);
}
Run Code Online (Sandbox Code Playgroud)

谢谢

pax*_*blo 5

如果你想要一个类似的序列5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4(起始点可以是该集合中的任何数字),你不需要花哨的循环结构.

您可以使用模运算符的常规循环,例如(伪代码,因为其中一个标记是language-agnostic):

start = 5
for i = 0 to 10
    use_i = (start + i) % 11
    // do something with use_i rather than i.
next i
Run Code Online (Sandbox Code Playgroud)

use_i将从5 迭代到4,在10之后循环回到0.