如果一个循环的条件在开始时不满足,则继续嵌套循环的一部分

Continue parts of nested loop if condition of one loop is not met at exactly the beginning

我正好有 3 层嵌套循环。如果输入恰好为 0,则其他 for 循环的执行应继续进行,因此正常的嵌套循环将不起作用 我试着想出一个解决方法,但我不知道如何让它发挥作用。欢迎任何类型的解决方案

我有 3 个输入,它们是某种“乘数”。如果值为 0,执行仍应继续。

示例:

int coni=2, conj=-2, conk=0;
//intermediate values that tell should it be negative or positive
//calculated from the beginning values, but ill get rid of the code
int ti=1,tj=-1,tk=0;

for(int i=0;i<(int)Math.abs(coni);i++){
    for(int j=0;i<(int)Math.abs(conj);j++){
        for(int k=0;i<(int)Math.abs(conk);k++){
            //will never get executed
            function(i*ti,j*tj,k*tk);
        }
    }
}

我想要发生的事情:

function(0,0,0);
function(0,-1,0);
function(1,0,0);
function(1,-1,0);

在您的 for loop 中设置 <= 而不是 < 将帮助您实现您想要的。

for(int i=0;i<=(int)Math.abs(coni);i++){
    for(int j=0;i<=(int)Math.abs(conj);j++){
        for(int k=0;i<=(int)Math.abs(conk);k++){
            //will never get executed
            function(i*ti,j*tj,k*tk);
        }
    }
}

这将执行

function(0,0,0)