java 嵌套循环未正确迭代

mfuanj7w  于 2023-03-21  发布在  Java
关注(0)|答案(1)|浏览(99)

我有一段代码,其中有两个循环'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时,'flag'才会为true,这是循环在此时不会中断的主要问题,因为'j'永远不会到达913,而是随机生成数字。此外,'break'不仅会中断内部循环,还会中断主循环。

dgenwo3n

dgenwo3n1#

在内部循环中有一个条件-if(flag)-如果为true,则从该循环中断。这就是内部循环不完成的原因。它不会“随机”运行。它的行为取决于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

EDIT,当i==995和j==583时,doSomething返回true。这就是你打破循环的时候。

相关问题