我的 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";}
}
}
}
}
这是我的驱动程序类:
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()
方法!预先感谢您!
最佳答案
您的问题是您尝试将 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
(例如String
),它存储对象引用。
假设 "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)
一样,它会进入Object #1
的 equals
函数(又名 String.equals(Object o)
)并比较以下值的值这两个 String
而不是比较它们的引用。
经验法则是 ==
仅适用于基元(int
、float
、long
、 double
、短
、字节
、字符
、 boolean
),但是对于其他所有内容,请使用 .equals
(可能必须被覆盖)。
关于java - 我需要创建一个访问器方法,该方法将返回最后一个被划掉的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49759788/