java - java中toString函数的问题(使用局部变量)

标签 java

我在java程序中有一个类,我使用toString函数来检索数据。 toString 检查同一个类中的私有(private)函数,该函数返回一个 int 值,用于显示不同类型的返回消息。~

问题是,如果我在字符串函数中使用局部变量,结果都很好,但如果我直接检查私有(private)函数的 if 语句,则该函数不会返回任何值。

private int computerTryHorizontalPlay() {

        int repeatedMyValueCount = 0;
        int repeatedYourValueCount = 0;
        int[] myPositions = new int[3];
        int[] yourPositions = new int[3];

        for (int a = 0; a < 3; a++) {
            int repeatedMyValue = 0;
            int repeatedYourValue = 0;
            int emptyFields = 0;
            int[] emptyPosition = new int[2];
            for (int b = 0; b < 3; b++) {
                if (jogoGalo[a][b] == 'X') {
                    repeatedMyValue++;
                } else if (jogoGalo[a][b] == 'O') {
                    repeatedYourValue++;
                }
                if (jogoGalo[a][b] == '-') {
                    emptyPosition[0] = a;
                    emptyPosition[1] = b;
                    emptyFields++;
                }
            }

            if (repeatedMyValue == 3 || repeatedYourValue == 3) {
                return 3;
            } else {
                if (emptyFields == 1) {
                    if (repeatedMyValue == 2) {
                        repeatedMyValueCount++;
                        myPositions[repeatedMyValueCount - 1] = emptyPosition[0];
                        myPositions[repeatedMyValueCount] = emptyPosition[1];
                    } else if (repeatedYourValue == 2) {
                        repeatedYourValueCount++;
                        yourPositions[repeatedYourValueCount - 1] = emptyPosition[0];
                        yourPositions[repeatedYourValueCount] = emptyPosition[1];
                    }
                }
            }
        }

        if (repeatedMyValueCount > 0) {
            jogoGalo[myPositions[0]][myPositions[1]] = 'X';
            return 2;
        } else if (repeatedYourValueCount > 0) {
            jogoGalo[yourPositions[0]][yourPositions[1]] = 'X';
            return 1;
        }

        return 0;
    }

这行不通!

    public String toString() {
        if(computerTryHorizontalPlay() == 3) {
            return "The game has already ended!";
        }
        else if(computerTryHorizontalPlay() == 2) {
            return "Computer won!";
        }
        else if(computerTryHorizontalPlay() == 1) {
            return "Computer defendeu!";
        }
        return null;
    }

这有效!

public String toString() {

        int horizontalFunctionValue = computerTryHorizontalPlay();

        if(horizontalFunctionValue == 3) {
            return "The game has already ended!";
        }
        else if(horizontalFunctionValue == 2) {
            return "Computer won!";
        }
        else if(horizontalFunctionValue == 1) {
            return "Computer defendeu!";
        }
        return null;
    }
}

最佳答案

toString() 必须是只读方法,即不允许有诸如更改对象状态之类的副作用。由于 computerTryHorizo​​ntalPlay() 是一个状态更改方法,因此不允许您从 toString() 调用它。

由于唯一的状态更改发生在最后一个 if 语句中,因此您可以更改代码,以便在从 toString() 调用时不执行播放,如下所示:

private int computerTryHorizontalPlay() {
    return computerTryHorizontalPlay(true);
}

private int computerTryHorizontalPlay(boolean doMove) {

    // lots of code here

    if (repeatedMyValueCount > 0) {
        if (doMove)
            jogoGalo[myPositions[0]][myPositions[1]] = 'X';
        return 2;
    } else if (repeatedYourValueCount > 0) {
        if (doMove)
            jogoGalo[yourPositions[0]][yourPositions[1]] = 'X';
        return 1;
    }

    return 0;
}

public String toString() {
    if(computerTryHorizontalPlay(false) == 3) {
        return "The game has already ended!";
    }
    else if(computerTryHorizontalPlay(false) == 2) {
        return "Computer won!";
    }
    else if(computerTryHorizontalPlay(false) == 1) {
        return "Computer defeated!";
    }
    return null;
}

关于java - java中toString函数的问题(使用局部变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56860446/

相关文章:

java - 比 while (reentrantLock.isLocked()) 等待更好的解决方案

java遍历集合时移除元素,怎样避免ConcurrentModificationException异常抛出

java - 如何克服 JFreeChart 问题?

java - org.hibernate.QueryException : could not resolve property: user of: it. besmart.models.Park

java - 如何不允许将重复项添加到二叉搜索树中?

java - Groovy 空格分隔的 HashMap 键

java - JPA2.1 - 调用返回 CLOB 的存储过程

java - Java8 中将列表分区为子列表

java - 如何调整 JOptionPane.showConfirmDialog 的大小

java - 如何屏蔽Applet打开的URL?