【发布时间】: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 < boardMatrix.length被评估为假。 -
我感觉迟钝。由于您是第一个回复的人,如果您创建答案,我会给您投票并接受您的答案
-
你可以接受已经写好的答案:-)
标签: java multidimensional-array nested-loops