我的java任务包括创建一个名为Qwixx的棋盘游戏。你掷骰子然后划掉游戏牌上的数字,那里有2行从2到12行,另外2行从12到2行。我的问题是创建访问器方法以获取每行的最后一个交叉编号。最后划掉的数字应该是游戏板右边最远的数字。这就是我对Player类的看法:
public class Player {
private String[][] gameBoard = new String[4][11];
private int lastCrossedOffR;
//default constuctor
public Player() {
lastCrossedOffR = 0;
initializeGameboard();
}
//get method to obtain the last number that has been crossed off
public int getLastCrossedOffR() {
for(int j = 0; j<gameBoard[0].length;) {
if (gameBoard[0][j] == "X") {
if (j == 0)
lastCrossedOffR = 2;
else if (j == 1)
lastCrossedOffR = 3;
else if (j == 2)
lastCrossedOffR = 4;
else if (j == 3)
lastCrossedOffR = 5;
else if (j == 4)
lastCrossedOffR = 6;
else if (j == 5)
lastCrossedOffR = 7;
else if (j == 6)
lastCrossedOffR = 8;
else if (j == 7)
lastCrossedOffR = 9;
else if (j == 8)
lastCrossedOffR = 10;
else if (j == 9)
lastCrossedOffR = 11;
else if (j == 10)
lastCrossedOffR = 12;
} else {
j++;
}
}
return lastCrossedOffR;
}
//method that initializes each row of the game board
public void initializeGameboard() {
for (int i = 0; i<gameBoard.length; i++) {
for (int j = 0; j<gameBoard[i].length; j++) {
if (i==0 || i==1) {
if (j==0) {gameBoard[i][j] = "2";}
else if (j==1) {gameBoard[i][j] = "3";}
else if (j==2) {gameBoard[i][j] = "X";} //i placed random x's to
else if (j==3) {gameBoard[i][j] = "5";} //test out the get
else if (j==4) {gameBoard[i][j] = "6";} //method
else if (j==5) {gameBoard[i][j] = "7";}
else if (j==6) {gameBoard[i][j] = "X";}
else if (j==7) {gameBoard[i][j] = "X";} //in my case, what the
else if (j==8) {gameBoard[i][j] = "10";} //get method should
else if (j==9) {gameBoard[i][j] = "11";} //return is: 9
else if (j==10) {gameBoard[i][j] = "12";}
} else if (i==2 || i==3) {
if (j==0) {gameBoard[i][j] = "12";}
else if (j==1) {gameBoard[i][j] = "11";}
else if (j==2) {gameBoard[i][j] = "10";}
else if (j==3) {gameBoard[i][j] = "9";}
else if (j==4) {gameBoard[i][j] = "8";}
else if (j==5) {gameBoard[i][j] = "7";}
else if (j==6) {gameBoard[i][j] = "6";}
else if (j==7) {gameBoard[i][j] = "5";}
else if (j==8) {gameBoard[i][j] = "4";}
else if (j==9) {gameBoard[i][j] = "3";}
else if (j==10) {gameBoard[i][j] = "2";}
}
}
}
}
这是我的Driver类:
public class Driver {
public static void main(String[] args) {
Player obj1 = new Player();
obj1.printGameboard(); //assume this method is already created in the Player class
System.out.print("The last crossed off number for the Red row is: " + obj1.getLastCrossedOffR());
}
}
这是我运行代码时需要显示的内容:
player's gameboard:
Red: 1 2 3 X 5 6 7 X X 10 11 12
Yellow: 1 2 3 4 5 6 7 8 9 10 11 12
Green: 12 11 10 9 8 7 6 5 4 3 2 1
Blue: 12 11 10 9 8 7 6 5 4 3 2 1
The last crossed off number for the Red row is: 9
请帮我弄清楚如何修复getLastCrossedOffR()
方法!提前谢谢!
答案 0 :(得分:1)
您的问题是,您要将String
与==
而不是equals
进行比较,请改用:
public int getLastCrossedOffR() {
for(int j = 0; j < gameBoard[0].length;) {
// was previously `gameBoard[0][j] == "X"`
if ("X".equals(gameBoard[0][j])) {
// ...
==
运算符比较变量中字面的内容。对于像int
这样的原语,这很好,因为它实际上存储了原始值。但是在Object
s String
s的情况下,它存储了对象引用。
想象一下,"X"
是String
,其中引用Object #1
和gameBoard[0][0]
存储了String
引用Object #2
。
调用"X" == gameBoard[0][0]
就像调用Object #1 == Object #2
一样,这是假的。 (除非您已将Object #1
存储在gameBoard[0][0]
)
但是调用"X".equals(gameBoard[0][0])
就像调用Object #1.equals(Object #2)
进入equals
Object #1
函数{aka String.equals(Object o)
)并比较值这两个String
而不是比较他们的参考文献。
经验法则是==
仅适用于基元(int
,float
,long
,double
,{{1} },short
,byte
,char
),但对其他所有内容使用boolean
(可能必须覆盖)。