【问题标题】:by boolean, how to change existing chars to be true通过布尔值,如何将现有字符更改为真
【发布时间】:2015-07-11 21:58:43
【问题描述】:

我的代码是这样的

 public class AsciiGrid
    {
        private static int ascrow = 0;
        private static int asccol = 0;
        private static char[][] myAsciiGrid; 
        private boolean [][] GridOccupied;
        private static char space = ' ';
.
.
.
.
.
        public String toString()
        {
                String[][] mystring = new String [ascrow+2][asccol+2];
                String space = " ";
                for (int i=0; i < ascrow+2; i++)
                {
                    for (int j = 0; j< asccol+2; j++)
                    {
                        if ((i==0)||(i==ascrow+1))
                            mystring[i][j] = "=";
                        else if ((j==0)||(j==asccol+1))
                            mystring[i][j] = "|";
                        else
                            mystring[i][j] = space;
                    }
                } 
                return mystring;
}
}

这将返回一个类似网格的东西,我将在其中放置一些图像。图像只会是三角形和矩形 就像:

===========        ===========
|         |        |         |
|   *     |        |   **    |
|   **    |        |   **    |
===========        ===========

在我把下一张图片放在那里之前,我想通过使用布尔值来检查空间是否已经被使用。我不希望它们重叠。所以我写的代码就像:

     private boolean placeable(int r,int c,int ascrow, int asccol){

}

但我不知道如何将我的处理代码更改为布尔值并测试空间是否在那里。 这是我的copyGrid:

private static char [][] copyGrid(char [][] myAsciiGrid)
    {
        char[][] newarray1 = new char [myAsciiGrid.length][myAsciiGrid[0].length];
        for (int i = 0; i < myAsciiGrid.length; i++)
            for(int j = 0; j < myAsciiGrid[0].length; j++)
                newarray1[i][j] = myAsciiGrid[i][j];
        return newarray1;
    }

我不确定这是否正确。

有人可以帮忙吗?谢谢

【问题讨论】:

  • 为什么不直接使用array[i][j].equals(" ") 来检查是否可以安全地替换而不重叠。
  • @Daniel Nugent - 大概他的形状有仍然不能重叠的“空”空间?
  • 我不知道为什么 OP 正在复制网格。但是好的问题是具体的:“我看到 的行为,我想要 ”会比“这对吗”更好,更“可回答”?
  • 您需要更好地定义您期望这个布尔值的含义。事实上,我怀疑通过这样做,您将弄清楚如何评估现有数据以确定其真实性,或者如何定义一个结构来为您跟踪它。
  • 你应该使用状态模式

标签: java arrays multidimensional-array boolean


【解决方案1】:

您的问题措辞相当糟糕,但据我了解,您需要一种方法来告诉您“网格”中是否有除空间之外的其他内容。也许像下面这样的东西可以帮助:

private boolean isEmpty() {
    boolean empty = true;
    for (int i=1;i<myAsciiGrid.length-1;i++) {
        for (int j=1;j<myAsciiGrid[0].length-1;j++) {
            if (!myAsciiGrid[i][j].equals(" ")) {
                return false;
            }
        }
    }
    return empty;
}

但我的理解可能是错误的,我想您自己会弄清楚以上内容

【讨论】:

    【解决方案2】:

    她的如何直接查看字符串的章程

    boolean filled;
    String str;
    if (str.charAt(character position starting at 0).equals(" "))
    filled = false;
    else
    filled = true;
    

    【讨论】:

      猜你喜欢
      • 2014-12-17
      • 1970-01-01
      • 2023-01-27
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 2013-07-29
      相关资源
      最近更新 更多