【问题标题】:My nested for loop isn't executing and I don't know why我的嵌套 for 循环没有执行,我不知道为什么
【发布时间】:2014-12-06 11:56:53
【问题描述】:

我的简单嵌套 for 循环没有执行。这段代码位于一个方法中。方法本身正在执行,并且它周围的代码正在执行,但嵌套循环没有执行。我尝试以相同的方法将代码复制并粘贴到不同的区域,但没有奏效。我尝试创建一个与该项目无关的不同类,并将其粘贴到那里并进行必要的调整,但没有成功。

所有其他必要的变量和数字信息都可以在代码和提供的结果中查看。

      System.out.println("This is just to show that variables do have actual numbers in them." +
                       "\n  basePointy =  " + basePointy + "\n  basePointx = " + basePointx + 
                       "\n  boardMatrix.length = " + boardMatrix.length + "\n  boardMatrix[0].length = " + boardMatrix[0].length + 
                       "\n  a[0][0] = " + a[0][0] + "\n  a[5][5] = " + a[5][5] + "\n");

      System.out.println("This is before the nested for loop");

      //This is the problem area.
      for(int yaxisCounter = basePointy + 185; yaxisCounter < boardMatrix.length; yaxisCounter++)
      {
         System.out.println("This is in the first part of the nested for loop");
         for(int xaxisCounter = basePointx + 221; xaxisCounter < boardMatrix[0].length; xaxisCounter++)
         {
            boardMatrix[yaxisCounter-basePointy-185][xaxisCounter-basePointx-221] = a[yaxisCounter][xaxisCounter];    
            System.out.println("This is in the second part of the nested for loop");
         } 
      }
      System.out.println("This is after the nested for loop");

这是我运行代码后得到的结果。

This is just to show that variables do have actual numbers in them.
  basePointy =  160
  basePointx = 46
  boardMatrix.length = 16
  boardMatrix[0].length = 300
  a[0][0] = -8345659
  a[5][5] = -8412480

This is before the nested for loop

This is after the nested for loop

注意它不打印“这是在嵌套 for 循环的第一部分。”和“这是嵌套 for 循环的第二部分。”但在嵌套 for 循环之前和之后打印/执行所有内容。

在此先感谢您阅读并提供帮助的人

【问题讨论】:

  • 因为yaxisCounter的值大于boardMatrix.length,所以yaxisCounter &lt; boardMatrix.length被评估为假。
  • 我感觉迟钝。由于您是第一个回复的人,如果您创建答案,我会给您投票并接受您的答案
  • 你可以接受已经写好的答案:-)

标签: java multidimensional-array nested-loops


【解决方案1】:

因为当你开始你的外循环时,你的 yaxisCounter = 160 + 185 = 345,你的循环条件是 yaxisCounter &lt; boardMatrix.length 这是假的,因为 16 不大于 345(即 345

【讨论】:

    【解决方案2】:

    因为第一个循环条件为假

    【讨论】:

      【解决方案3】:

      试试这个

      for (int yaxisCounter = basePointy + 185; yaxisCounter < boardMatrix.length; yaxisCounter++) {
          System.out.println("This is in the first part of the nested for loop");
          for (int xaxisCounter = basePointx + 221; xaxisCounter < boardMatrix[yaxisCounter].length; xaxisCounter++) {
              boardMatrix[yaxisCounter - basePointy - 185][xaxisCounter - basePointx - 221] = a[yaxisCounter][xaxisCounter];
              System.out.println("This is in the second part of the nested for loop");
          }
      }
      

      【讨论】:

      • 不是因为 yaxisCounter 大于 boardMatrix.length。
      • 我将在几分钟后删除。也许
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2018-06-05
      • 2017-09-20
      • 2016-01-10
      • 2017-08-19
      • 1970-01-01
      相关资源
      最近更新 更多