char数组没有正确比较java

时间:2013-11-15 03:53:02

标签: java arrays char

这有点奇怪,但我的java代码不能正确比较字符。

我有这段代码循环通过二维字符串数组并用'D'替换每个'我'

for (i = 0; i < 12; i++)
    {
        for (j = 0; j < 12; j++)
        {
            if (activeGrid[i][j] == 'I')
            {
                activeGrid[i][j] = 'D';
                System.out.println("Changing [" + i + "][" + j + "]");
            }
            else
            {
                System.out.println("No I in [" + i + "][" + j + "]");
            }
        }
    }

然而,它似乎没有正常工作,这是一个示例运行:

   0 1 2 3 4 5 6 7 8 9 A B
   _ _ _ _ _ _ _ _ _ _ _ _
0 | | | | | | | | | | | | |
1 | | | | | | | | | | | | |
2 | | | | | | | | | | | | |
3 | | | | | | | | | | | | |
4 | | | | | | | | | | | | |
5 | | | | | |D| | | | | | |
6 | | | | | | | | | | | | |
7 | | | | | | | | | | | | |
8 | | | | | | | | | | | | |
9 | | | | | | | | | | | | |
10 | | | | | | | | | | | | |
11 | | | | | | | | | | | | |

   0 1 2 3 4 5 6 7 8 9 A B
   _ _ _ _ _ _ _ _ _ _ _ _
0 | | | | | | | | | | | | |
1 | | | | | | | | | | | | |
2 | | | | | | | | | | | | |
3 | | | | | | | | | | | | |
4 | | | | | |I| | | | | | |
5 | | | | |I|D|I| | | | | |
6 | | | | | |I| | | | | | |
7 | | | | | | | | | | | | |
8 | | | | | | | | | | | | |
9 | | | | | | | | | | | | |
10 | | | | | | | | | | | | |
11 | | | | | | | | | | | | |

(我删除了“不在我......”的部分以供阅读)

然后它拒绝从这里更新,没有多少次我试图得到它,它不会改变我对D的。只是为了更加混乱,有时它会改变我的一些或全部,然后在以后卡住

我也看到与代码的其他部分类似的东西,一部分检查与D相邻的每个单元并决定D是否应该扩散,不扩散的因素之一是如果在D中有D或I细胞已经存在,但是在某些细胞中我看到每次按下Enter键时从D切换到I的值,我也看到随机的我出现在D的海洋中

有什么想法吗?

[编辑]

活动网格初始化如下:

public char[][] setActiveGrid (int x, int y)
{
    char grid[][] = new char [12][12];
    int Px;
    int Py;
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            grid[i][j] = ' ';
        }
    }
    grid[x][y] = 'D';
    do
    {
        Px = (int) Math.ceil((Math.random() * 12) - 1);
        Py = (int) Math.ceil((Math.random() * 12) - 1);
    } while (Px == x && Py == y);
    //grid[Px][Py] = 'P';

    return grid;
}

它只会被改变:

public char[][] spreadDisease (char regionGrid[][], char activeGrid[][])
{
    char regionFlag;
    int i;
    int j;
    int xDiff;
    int yDiff;

    for (i = 0; i < 12; i++)
    {
        for (j = 0; j < 12; j++)
        {
            if (activeGrid[i][j] == 'D')
            {
                regionFlag = regionGrid[i][j];

                for (xDiff = -1; xDiff < 2; xDiff = xDiff + 2)
                {
                    if (regionGrid[(i+xDiff)][j] == regionFlag && (activeGrid[(i+xDiff)][j] != 'D' || activeGrid[(i+xDiff)][j] != 'I'))
                    {
                        activeGrid[(i+xDiff)][j] = 'X';
                    }
                    else
                    {
                        activeGrid[(i+xDiff)][j] = 'Y';
                    }
                }
                for (yDiff = -1; yDiff < 2; yDiff = yDiff + 2)
                {
                    if (regionGrid[i][(j+yDiff)] == regionFlag && (activeGrid[i][(j+yDiff)] != 'D' || activeGrid[(i+xDiff)][j] != 'I'))
                    {
                        activeGrid[i][(j+yDiff)] = 'X';
                    }
                    else
                    {
                        activeGrid[i][(j+yDiff)] = 'Y';
                    }
                }
            }
        }
    }
    return activeGrid;
}

regionGrid是一个2D char数组,填充了随机分类的+ - *和〜。 D传播的规则是:

  • 如果与D相邻的单元格具有与D相同的区域char,则它们被设置为D
  • 否则将它们设置为I,然后设置为D next run。

1 个答案:

答案 0 :(得分:0)

由于它工作一次然后看起来不再起作用,因此几乎可以肯定您没有正确地交换缓冲区以用于后续迭代。您要使用的算法是:

Declare two 12x12 regions a and b.
Initialize a
number_of_generations = 0
loop
  Using a as source, compute spread with b as destination
  display b
  break if ++number_of_generations == limit
  Using b as source, compute spread with a as destination
  display a
  break if ++number_of_generations == limit
end loop