简体   繁体   English

Java 2维布尔数组值

[英]Java 2Dimensional Boolean Array Values

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. 首先,我将数组中的所有值都设置为false,然后将其打印到屏幕上。 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");
    }
  }
}

This is bad: 这不好:

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

you are using the assignment operator = instead of the comparison operator == 您正在使用赋值运算符=而不是比较运算符==

You could change it to 您可以将其更改为

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

or nicer 或更好

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

or even nicer: 甚至更好:

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

Try this: 尝试这个:

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

With booleans you can do like 使用布尔值,您可以喜欢

if(boolean field)

You have used = assigment instead of == comparison. 您已使用=分配而不是==比较。

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

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