嵌套循环没有正确迭代

时间:2015-01-13 07:48:23

标签: java loops logic nested-loops

我有这段代码,其中有两个循环' i'和' j'。

' J'是内部循环变量,它应该在单循环外循环上运行999到100。

但它随机运行, 假设i = 999

J = 912

J = 911

J = 910

J = 909

J-908

J = 907

J = 906

然后突然内循环退出,从外循环中减少1并使i = 998

然后开始' j'环

J = 908

J = 907

J = 906

J = 905

J = 906

J = 905

J = 904

J = 903

J = 902

然后退出内圈................

    int product=0;
    mainloop:
    for(int i=999;i>99;i--){
        for(int j=999;j>99;j--){
            boolean flag= doSomething(i*j);
            if(flag){
                product=i*j;
                System.out.println("Digits are: "+i+" and "+j);
                break mainloop;
            }
        }
    }

 public  boolean doSomething(int product){
        String original= Integer.toString(product),reverse="";
        int length = original.length();
        for ( int i = length - 1; i >= 0; i-- )
            reverse = reverse + original.charAt(i);
        return (original.equals(reverse));


    }

为什么会这样?为什么内环不能完成从999到100的循环?

编辑:要清除,标记'只有当j = 913且i = 993时才会成立,这是循环在这一点上不会中断的主要问题,因为' j'永远不会到913,但它会随机产生数字。而且还会突然出现#9;将打破主循环,而不仅仅是内循环。

1 个答案:

答案 0 :(得分:3)

你在内循环中有一个条件 - if(flag) - 如果为真,则从该循环中断开。这就是内循环没有完成的原因。它不会“随机”运行。它的行为取决于doSomething(i*j)方法调用的结果。

使用一些额外的调试打印件以及您声称拥有的doSomething的实现来运行代码:

  int product=0;
  mainloop:
  for(int i=999;i>99;i--){
    System.out.print ("\n"+i+":");
    for(int j=999;j>99;j--){
      System.out.print (j+",");
      boolean flag= doSomething(i*j);
      if(flag){
        product=i*j;
        System.out.println("\nDigits are: "+i+" and "+j);
        break mainloop;
      }
    }
  }

  public static boolean doSomething(int k)
  {
      return k == 913*993;
  }

给出预期的输出:

999:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
998:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
997:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
996:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
995:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
994:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
993:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,979,978,977,976,975,974,973,972,971,970,969,968,967,966,965,964,963,962,961,960,959,958,957,956,955,954,953,952,951,950,949,948,947,946,945,944,943,942,941,940,939,938,937,936,935,934,933,932,931,930,929,928,927,926,925,924,923,922,921,920,919,918,917,916,915,914,913,
Digits are: 993 and 913

编辑,{i}发布的doSomething在i == 995和j == 583时返回true。那是你打破循环的时候。