【问题标题】:My for loop keeps going despite the condition met尽管满足条件,我的 for 循环仍在继续
【发布时间】:2023-04-02 22:50:01
【问题描述】:

n 在其循环结束时等于 2,但在最后一个“if”语句中它以某种方式为 3。) 无法弄清楚它在哪里改变。 因为最后一个 if 条件没有得到满足,所以 for 循环继续运行而不是返回 true (for(int i=0;i<Board[i].length;i++)) 和 ) 得到一个越界异常。

所以两个问题-

  1. 你能看出 n 的变化吗?
  2. 为什么我的for循环在满足条件的情况下仍然继续? Board[i].length=3 并且循环继续运行并给我一个越界异常,而不是退出 for 循环并在它之后返回 false。
public boolean ColChecker() {
    int n=0;
    // create boolean array and set all values to false
    boolean[] isExist = new boolean[10];
    for(int i=0;i<isExist.length;i++) 
        isExist[i]=false;
        
    //loop over columns and test using whosThereCol method
    for(int i=0;i<Board[i].length;i++) {
        for(int col=0;col<Board[0][0].getLength();col++) {
            for(n=0;n<Board.length;n++) 
                Board[n][i].whosThereCol(col,isExist);
                    
            //if array still has missing values, column incomplete - return false
            for(int j=1;j<10;j++) 
                if(!isExist[j]) 
                    return false;
            //if no missing values, initialize array to false values for next iteration of for loop
            for(int j=1;j<10;j++) 
                isExist[j]=false;
                            
                    
        // "if" statement checks if this is the last column in the last square, if so, we passed all the tests. return true     
        if(i+1==Board[0].length&&col+1==Board[0][0].getLength()&&n+1==Board.length)
            return true;                
        }
    }
    return false;
}

【问题讨论】:

    标签: java arrays for-loop


    【解决方案1】:

    这是因为for 语句末尾的n++ 也会在最后一个循环之后执行。

    考虑这段代码:

    int i;
    for (i = 0; i < 10; i++) {
        System.out.println(i);
    }
    System.out.println("=========");
    System.out.println(i);
    

    这将输出:

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    =========
    10
    

    【讨论】:

    • 哇,这真的让我很困惑。现在不知道如何使用 for 循环,其他 for 循环如何保持在界限内?为什么我的 "i" 和 "col" 变量按预期运行并保持低于界限 1 的值而 n 没有?
    【解决方案2】:

    通常你不需要最后的条件。因为你也可以简单地说当所有 3 个循环都运行完时: return true; 我首先注意到的错误是,您将 For 循环的顶部与 Board[i] 进行比较,但比较值再往下是 Board[0]。因此,如果 Board[i] 的长度不是始终相同,那么您的条件就不可能成立。

    您还应该将您的 For 循环顺序更改为:

     for(n=0; n<Board.length; n++){
                for(int i=0;i<Board[n].length;i++) {
                    for(int col=0;col<Board[n][i].getLength();col++) {
                        Board[n][i].whosThereCol(col,isExist);
                    }
                    //Your further Code
                }
            }
    

    【讨论】:

    • 您对 Board[i] 的看法是正确的,谢谢,我会改变它。当所有 3 个循环都运行完时,我怎么说:返回 true ?你也明白为什么n等于for循环条件而其他变量保持
    • 您可以将最后一行从 return false; 更改为 return true; 我想如果您使用 {} 编写所有循环,这对您将非常有帮助,这样循环开始时会更加清晰,并且结束。
    • 我刚刚删除了 if 并在循环之外添加了 return true,正如你所说的。但我仍然得到一个越界异常。而不是结束 for 循环并返回 true 它只是继续进行
    • 那是因为您的循环顺序错误。您应该将 n 循环放在顶部,然后使用 i 循环 for(int i =0; i&lt;Board[n].length; i++) 进行迭代,此时您的停止条件没有意义,因为 i 可以高于 'Board[n].lenght'
    • 更改订单不会执行相同的操作,它不会做我希望它对你的代码做的事情
    猜你喜欢
    • 2012-09-16
    • 2020-05-18
    • 1970-01-01
    • 2017-02-03
    • 2020-11-29
    • 1970-01-01
    • 2019-08-09
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多