我有一個數組:元素多維數組的,爪哇
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 [email protected]"
我想它打印"There is a bomb at 3, 2"
可能很簡單,但我畫的是空白。而不是拉回元素內部的元素,我想要元素索引(我假設)。請halp!
比較'String'值與'String'的'equals'方法,而不是用''==操作符。你在第二種情況下做得正確。 – rgettman