【问题标题】:Java: Wrong variable increasing?Java:错误的变量增加?
【发布时间】:2013-04-30 12:29:16
【问题描述】:

这个小sn-p 代码由于某种原因不能正常工作。如果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

【问题讨论】:

  • 那么for (int r = 0; r &lt; gridRow-1; r++){ 呢?
  • 现在我觉得自己很愚蠢。对不起。那为什么它只通过它两次呢?第 4 步应该只调用一次,但由于某种原因,这是唯一需要执行两次的步骤,即使代码几乎与其他 3 步完全相同...
  • 我会建议你调试你的代码。我不能通过查看这段代码 sn-p 来告诉你为什么。但是你在哪里打印值的块应该仔细看看。

标签: java variables if-statement for-loop increment


【解决方案1】:

如果我理解正确,最后两个输出,其中 8 和 9 相加是错误的。

问题是,您一直在使用更新的 numLocation 搜索网格,而这个 numLocation 位于网格的一部分中,而不是搜索 jet。

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

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

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

并在最里面的for循环之后插入这个(带有i)

break label;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 2015-05-26
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多