我的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 1The 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)一样,后者进入equals(也称为Object #1)的String.equals(Object o)函数并比较这两个String的值,而不是比较它们的引用。经验法则是==仅适用于基元(int,float,long,double,short,byte,char,boolean),但使用(可能必须覆盖)其他所有内容。关于java - 我需要创建一个访问器方法,该方法将返回被舍去的最后一个数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49759788/ 10-12 20:51