0

我有一个数组:

public String[][] bombBoard = new String[9][9];

这个数组填充了值 (*),但随机还有一些其他值 (@)。

稍后在程序中我想告诉用户(@)在哪里,如果附近有:

public void loadNumbers()
{
    try
    {
        if (bombBoard[bombRow][bombCol] == "@") 
        {
            System.out.println("You have landed on a bomb!");
            //break
        }
        else if (bombBoard[bombRow+1][bombCol].equals("@"))
        {
            System.out.println("There is a bomb in the vicinity!" + bombBoard[bombRow+1][bombCol]);
        }
        else if (bombBoard[bombRow-1][bombCol] == "@")
        {
            System.out.println("There is a bomb in the vicinity!" + bombBoard[bombRow-1][bombCol]);
        }
        else if (bombBoard[bombRow][bombCol+1] == "@")
        {
            System.out.println("There is a bomb in the vicinity!" + bombBoard[bombRow][bombCol+1]);
        }
        MORE CODE...
    }

}

它打印:"There is a bomb in the vicinity!@"

我要打印"There is a bomb at 3, 2"

可能很简单,但我正在画空白。而不是拉回元素内部的内容,我想要元素索引(我想)。请停下来!

4

2 回答 2

1

我假设您只想打印出@? 如果是这样,您可以这样做:

System.out.println("There is a bomb at " + bombRow + ", " + bombCol + 1);

对其他条件使用相同的模式。此外,您想使用.equals而不是比较字符串==(后者仅比较引用)。

于 2013-11-14T19:48:45.290 回答
0

尝试

 System.out.println("There is a bomb in the vicinity!" + (bombRow+1).toString() +"," +bombCol.toString());
于 2013-11-14T19:49:51.067 回答