简体   繁体   English

布尔值不会破坏语句

[英]Boolean doesn't break for statement

I am new to the forum AND new to programming. 我是这个论坛的新手,也是编程的新手。 I apologize in advance if my questions are really basic but I am really new to this and I had to learn a lot in a really short time, so I may miss a couple of concepts. 如果我的问题确实很基础,我会提前道歉,但是我真的是新手,我必须在很短的时间内学到很多东西,所以我可能会错过一些概念。

On to my question, I have a method who is supposed to check if a matrix still have spaces available and stop if it finds one. 关于我的问题,我有一个方法应该检查矩阵是否仍然有可用空间,如果找到一个,则停止。

The matrix has been initialized like this (I know it could be in one line, but the matrix is created within the constructor of an object) 矩阵已经像这样初始化(我知道它可能在一行中,但是矩阵是在对象的构造函数中创建的)

protected char[][] matJeu = null;
matJeu = new char[10][10];

It has then been filled with spaces like this ' ' with a for statement and it works just fine 然后用for语句填充这样的''空格,它就可以正常工作

Here is the method 这是方法

public boolean checkIfFull()
{
    boolean full = true;
    for (int i=0;i<matJeu.length || !full;i++)
    {
        for (int j=0;j<matJeu[i].length || !full ;j++)
        {
            if(matJeu[i][j]==' '){
                full = false;
            }
        }
    }
    return full;
} 

The problem is that when the full boolean turns to false, the for loops doesn't break and ends up by causing an ArrayOutOfBounds exception. 问题在于,当完整的布尔值变为false时,for循环不会中断,并通过引发ArrayOutOfBounds异常而结束。 If the matrix is full, it simply returns true. 如果矩阵已满,则仅返回true。 So I may be missing the correct way to cause a boolean to break a for loop. 因此,我可能缺少使布尔值打破for循环的正确方法。

Thank you in advance for your time. 预先感谢您的宝贵时间。

The conditional part of the loop will cause the loop to continue as long as it returns true . 只要返回true ,循环的条件部分将导致循环继续。

As soon as full=false is hit, then !full==true , and the conditional statement will always evaluate to true ( anything || true == true ), essentially putting you in an infinite loop with your current code. 一旦点击full=false ,然后!full==true ,条件语句将始终为trueanything || true == true ),这实际上使您陷入了当前代码的无限循环。

To break your for loop, you need the conditional part to evaluate to false . 要打破for循环,您需要将条件部分评估为false

I'm not sure what you intend your loops to do, as stopping at the first space character doesn't seem like what you want based on the previous paragraph. 我不确定您打算执行循环操作的原因,因为在第一个空格字符处停止似乎不像您根据上一段所希望的那样。

The problem is the or condition on your for loops. 问题是您的for循环上的or条件。

for (int i=0;i<matJeu.length || !full;i++)

Here, the loop is going to continue executing so long as at least one of the conditions is true. 在这里,只要至少一个条件为真,循环就将继续执行。

Because i<mathJeu.length is still true, the loop keeps executing, regardless of whether full is true or false . 因为i<mathJeu.length仍然为true,所以无论fulltrue还是false ,循环都会继续执行。

Instead - what you want is an and condition. 相反,您想要的是and条件。

for (int i=0;i<matJeu.length && full;i++)

This will tell it 'keep looping so long as it's below the array length AND we've detected it's still full'. 这将告诉它“只要循环长度小于数组长度并且我们检测到它仍已满,就保持循环”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM