0

I'm having a problem with my 2 dimensional boolean array. (or it may be with the logic of printing out the values). I set all of the values in the array to false in the beginning, and then later i print out the values to the screen. When i print them out, they all come up as true.

x=20;
y=10;
boolArray = new boolean[x][y];

for(int c=0;c<x;c++)
{
  for(int i=0;i<y;i++)
  {
    boolArray[c][i] = false;
  }
}

System.out.println("2D Boolean Array:");

for(int a = 0; a < boolArray.length; a++)
{
  for(int b = 0; b < boolArray[a].length; b++)
  {
    if(boolArray[a][b] = true)
    {
      System.out.print("T");
    }
    else if(boolArray[a][b] = false)
    {
      System.out.print("F");
    }
  }
}
4

2 回答 2

1

这是不好的:

if(boolArray[a][b] = true)
    {
      System.out.print("T");
    }
    else if(boolArray[a][b] = false)
    {
      System.out.print("F");
    }

您正在使用赋值运算符=而不是比较运算符==

你可以把它改成

if(boolArray[a][b] == true)
//...
else if(boolArray[a][b] == false)

或者更好

if(boolArray[a][b])
//...
else if(!boolArray[a][b])

甚至更好:

if(boolArray[a][b])
//...
else 
于 2012-12-04T14:51:11.583 回答
0

尝试这个:

    if(boolArray[a][b])
    {
      System.out.print("T");
    }
    else
    {
      System.out.print("F");
    }

使用布尔值,您可以这样做

if(boolean field)

您使用=了分配而不是==比较。

于 2012-12-04T14:51:02.523 回答