-6

我收到无法修复的错误。我在这些行中收到大约 8 个错误:

        if (weight [1] + weight[4] + weight[7] == twoWeights){
        if(weight[1]==0){
            return 1;

        else if (weight [4] == 0)
            return 4;

        else 
            return 7;

    }
    if (weight [2] + weight[5] + weight[8] == twoWeights){
        if(weight[2]==0)
            return 2;

        else if (weight [5] == 0)
            return 5;

        else 
            return 8;

    }
    if (weight [0] + weight[4] + weight[8] == twoWeights){
        if(weight[0]==0)
            return 0;

        else if (weight [4] == 0)
            return 4;

        else 
            return 8;

    }                       
    if (weight [2] + weight[4] + weight[6] == twoWeights){
        if(weight[2]==0)
            return 2;

        else if (weight [4] == 0)
            return 4;

        else 
            return 6;

    }                       
    return -1;
}

int getRandomSquare(){
    boolean gotEmptySquare = false;
    int selectedSquare = -1;

    do {
        selectedSquare = (int) (Math.random() * 9);
        if (squares[selectedSquare].getLabel().equals("")){
            gotEmptySquare = true;
        }
    }
    while (!gotEmptySquare);
        return selectedSquare;
    }
    void highlightWinner(int win1; int win2; int win3) {
        squares [win1].setBackground(Color.CYAN);
        squares [win2].setBackground(Color.CYAN);
        squares [win3].setBackground(Color.CYAN);
    }
    void endTheGame (){
        newGameButton.setEnabled(true);
        for(int i=0;i<9;i++){
            squares[i].setEnabled(false);
        }
    }
}

}

错误是:

TicTacToe.java:213: 'else' 没有 'if' else if (weight [4] == 0) ^

TicTacToe.java:256: ';' 预期 int getRandomSquare(){ ^

TicTacToe.java:269: 非法开始表达式 void highlightWinner(int win1; int win2; int win3) { ^

TicTacToe.java:269: ';' 预期 void highlightWinner(int win1; int win2; int win3) { ^

TicTacToe.java:269: ';' 预期 void highlightWinner(int win1; int win2; int win3) { ^

TicTacToe.java:274: 非法的表达式开头 void endTheGame (){ ^

TicTacToe.java:274: ';' 预期无效 endTheGame (){ ^

4

2 回答 2

2

上面代码的第二行多了一个{

更改if(weight[1]==0){if(weight[1]==0),因为您没有关闭左大括号。如果您在进行此更改后仍然看到错误,请举起整个班级。我怀疑您没有正确打开或关闭牙套。

于 2013-02-10T19:44:13.250 回答
0
if(weight[1]==0){
    return 1;

else if (weight [4] == 0)
    return 4;

您需要先关闭大括号,然后才能开始另一个 if/else if。或者如果 if 后面只有一行,则去掉左大括号。

于 2013-02-10T19:44:15.560 回答