Java 我需要创建一个访问器方法,它将返回最后一个被划掉的数字

Java 我需要创建一个访问器方法,它将返回最后一个被划掉的数字,java,Java,我的java任务包括创建一个名为Qwixx的棋盘游戏。你掷骰子并在游戏板上划掉数字,其中有2行从2-12开始,另外2行从12-2开始。我这里的问题是创建访问器方法来获取每行的最后一个被划掉的数字。最后一个划掉的数字应该是游戏板右侧最远的数字。这是我为玩家类准备的: public class Player { private String[][] gameBoard = new String[4][11]; private int lastCrossedOffR; //d

我的java任务包括创建一个名为Qwixx的棋盘游戏。你掷骰子并在游戏板上划掉数字,其中有2行从2-12开始,另外2行从12-2开始。我这里的问题是创建访问器方法来获取每行的最后一个被划掉的数字。最后一个划掉的数字应该是游戏板右侧最远的数字。这是我为玩家类准备的:

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()
方法!提前谢谢你

您的问题是,您试图将
字符串
=
进行比较,而不是
等于
,请使用以下方法:

public int getLastCrossedOffR() {
    for(int j = 0; j < gameBoard[0].length;) {
        // was previously `gameBoard[0][j] == "X"`
        if ("X".equals(gameBoard[0][j])) {
            // ...
public int getLastCrossedOffR(){
对于(int j=0;j
=
运算符比较变量中的值。对于
int
s这样的基本体,这是可以的,因为它确实存储基本体值。但是对于
对象
s,比如
字符串
s,它存储对象引用

假设
“X”
是一个
字符串
,引用
对象1
游戏板[0][0]
存储
字符串
引用
对象2

调用
“X”==gameBoard[0][0]
就像调用
对象1==Object[2
一样,这是错误的。(除非您在
gameBoard[0][0]
中存储了
对象1

但是调用
“X”.equals(gameBoard[0][0])
就像调用
对象1.equals(对象2)
一样,它进入
对象1
(aka
字符串.equals(对象o)
)的
equals
函数,比较这两个
字符串的值,而不是比较它们的引用


经验法则是
==
仅适用于原语(
int
float
long
double
short
byte
char
boolean
),但使用
.equals
(可能必须重写)对于其他所有内容。

请首先修复代码格式,您的
getLastCrossedOffR()
方法有三个开始括号,但只有一个结束括号。此代码无法编译。噢,感谢您指出这一点!我更正了它是的,我没有考虑等号()方法,因为我正在比较两个字符串…非常感谢,访问器方法现在可以工作了!