java,for statement:访问循环外的递增值

时间:2016-01-29 22:49:12

标签: java for-loop

for(int i = 0 ; i < Tri.length; i++)
    for(int v = 1; v < Tri.length; v++)
    {
        boolean plz = Tri[i].compareColors(Tri[v]);
        if (v == i)
            continue;
        if (plz == true)
            System.out.println("Triangle " + i + " is equal to  Triangle " + v + " " + plz);
    }

我需要在循环之外使用i的值。目前,print语句比我需要的循环语言多21倍。有什么办法我可以访问&#39; i&#39;将我的所有代码都放在for循环中

2 个答案:

答案 0 :(得分:0)

要在循环外访问i,请将第一行更改为:

int i = 0;
for(; i < Tri.length; i++)

答案 1 :(得分:0)

您可以使用while loop

int i = 0;
int v = 1;
while (i < Tri.length) {
 while (v < Tri.length) {
   boolean plz= Tri[i].compareColors(Tri[v]);
            if(v==i)
            continue;
            if(plz == true )
                System.out.println("Triangle " + i + " is equal to  Triangle "+ v+ " "+  plz);
  v++;
 }
 i++;
}