Java:变量增加错误?

标签 java variables if-statement for-loop increment

由于某种原因,这个小代码片段无法正常工作。它应该做的是,如果i小于colLength,此时为2,这意味着它应该在输入7后停止。出于某种原因它一直持续到数组末尾。

为什么会一直这样?我没有任何增加 r 的代码吗?

//this is a 5 step process, this is the 4th
if (stepMaker == 4 && numLocation < totalSteps){ 
    //looking through the array for the last number used in step 3, this works
    for (int r = 0; r < gridRow-1; r++){ 
        for (int c = 0; c < gridCol-1; c++){ // still looking
            //using 5 instead of numLocation works, numLocation keeps going however... why?
            if(grid[r][c] == (numLocation)) {
                int x = 1;
                for(int i = 0; i < colLength; i++){ 
                    grid[r + x][c] = numLocation + 1; 
                    System.out.println("x=" + x + " // " +
                                       "numLocation=" + numLocation + " // " +
                                       "r=" + r + " // " +
                                       "c=" + c + " // " +
                                       "stepMaker=" + stepMaker + " // " + 
                                       "colLength=" + colLength + " // " +
                                       "rowLength=" + rowLength);
                    numLocation++;
                    for (int xx = 0; xx < gridRow; xx++){
                        for (int yy = 0; yy < gridCol; yy++){
                            System.out.print(grid[xx][yy] + " ");
                        }
                        System.out.println("");
                    }
                    x++;
                }
            }
        }
    }
    //colLength++;
    stepMaker++;
}

这是输出:

x=1 // numLocation=5 // r=2 // c=2 // stepMaker=4 // colLength=2 // rowLength=3
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 5 4 3 0 0 
0 0 6 1 2 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
x=2 // numLocation=6 // r=2 // c=2 // stepMaker=4 // colLength=2 // rowLength=3
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 5 4 3 0 0 
0 0 6 1 2 0 0 
0 0 7 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
x=1 // numLocation=7 // r=4 // c=2 // stepMaker=4 // colLength=2 // rowLength=3
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 5 4 3 0 0 
0 0 6 1 2 0 0 
0 0 7 0 0 0 0 
0 0 8 0 0 0 0 
0 0 0 0 0 0 0 
x=2 // numLocation=8 // r=4 // c=2 // stepMaker=4 // colLength=2 // rowLength=3
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 5 4 3 0 0 
0 0 6 1 2 0 0 
0 0 7 0 0 0 0 
0 0 8 0 0 0 0 
0 0 9 0 0 0 0 
rowLength = 3   //   colLength = 2

最佳答案

如果我理解正确的话,最后两个输出(添加 8 和 9)是错误的。

问题是,您继续搜索具有更新的 numLocation 的网格,而该 numLocation 位于网格的一部分中,而未搜索 jet。

解决方案是在找到 numLocation 并进行更改后中断外部循环(带有 r 和 c 的循环)。

为此,您需要在第一个 for 之前添加一个标签:

label: for (int r = 0; r < gridRow-1; r++){...

并将其插入到最里面的 for 循环之后(带有 i)

break label;

关于Java:变量增加错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16404029/

相关文章:

java - 通过通用类型获取组件

java - 如何在 JPA 域模型中实现状态设计模式

python - 如何在图片文件名和循环中使用变量来使用 pygame 打开多张图片而不会使代码困惑

php - 如何为 html 元素提供从 php 变量获取的 ID?

c# - "Inline temporary variable"(IDE0019) 修复在与接口(interface)一起使用时导致语法错误

c# - 我怎样才能在转发器中做一个 if 语句

php - 如何获得更多的变量值?

java - 我的服务在待机状态下运行,但前提是已连接 USB。为什么?

java - 为什么我的 equals 方法不起作用?

php - 如何将 include(php 文件) 放入变量?