我了解这似乎是我所见过的关于C ++和python的常见问题,但似乎找不到解决我的Java问题的解释。

附上我的程序代码,因为我还很陌生并且正在学习如何精简它,所以相当长。

import java.util.Scanner;

public class TicTacToe {
    public static void main(String[] args) {
    boolean gameActive = true;
    boolean player1turn = true;
    String board[][]= {{" ","|"," ","|"," "},{"-","-","-","-","-",},{" ","|"," ","|"," "},{"-","-","-","-","-",},{" ","|"," ","|"," "}};

    commands();
    printBoard(board);

    while (gameActive == true) {
        if (player1turn == true) {
            System.out.println("\nPlayer 1, noughts turn. Please choose a position.");
        } else {
            System.out.println("\nPlayer 2, crosses turn. Please choose a position.");
        }
        String position = null;

        while ("retry".equals(changePosition(position, player1turn, board))) {
            Scanner keyboard = new Scanner (System.in);
            position = keyboard.nextLine();

            if ("help".equalsIgnoreCase(position)) {
                commands();
            }
            else if ("turn".equalsIgnoreCase(position)){
                turn(player1turn);
            }
            else if ("board".equalsIgnoreCase(position)){
                printBoard(board);
            }
            else {
                 if (!"retry".equalsIgnoreCase(changePosition(position, player1turn, board))) {
                     System.out.println(changePosition(position, player1turn, board));
                     if (player1turn == true) {
                         player1turn = false;
                     }
                     else if (player1turn == false) {
                         player1turn = true;
                     }
                 }
            }
        }
    }
}

public static void commands() {
    System.out.println("\nPlayer 1 is noughts, O");
    System.out.println("Player 2 is crosses, X");
    System.out.println("\nThe board uses a grid format");
    System.out.println("\"A1\" is the top-left position");
    System.out.println("\"A2\" is the top-middle position");
    System.out.println("\"A3\" is the top-right position");
    System.out.println("\"B1\" is the middle-left position");
    System.out.println("\"B2\" is the middle position");
    System.out.println("\"B3\" is the middle-right position");
    System.out.println("\"C1\" is the bottom-left position");
    System.out.println("\"C2\" is the bottom-middle position");
    System.out.println("\"C3\" is the bottom-right position");
    System.out.println("\n\"help\" will reveal this help message");
    System.out.println("\"board\" will reveal the board");
    System.out.println("\"turn\" will reveal whos turn it is");
}

public static void turn(boolean player1turn) {
    if (player1turn == true) {
        System.out.println("\nPlayer 1, noughts turn.");
    } else {
        System.out.println("\nPlayer 2, crosses turn.");
    }
}

public static void printBoard(String[][] board) {
    System.out.print("\n");
    for (int x=0;x<5;x++) {
        for (int y=0;y<5;y++) {
            System.out.print(board[x][y]+"");
        }
        System.out.println();
    }
}

public static String changePosition(String position, boolean player1turn, String[][] board) {
    if (("A1".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[0][0])) {
            board[0][0] = "O";
            return board[0][0];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("B1".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[2][0])) {
            board[2][0] = "O";
            return board[2][0];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("C1".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[4][0])) {
            board[4][0] = "O";
            return board[4][0];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("A2".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[0][2])) {
            board[0][2] = "O";
            return board[0][2];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("B2".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[2][2])) {
            board[2][2] = "O";
            return board[2][2];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("C2".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[4][2])) {
            board[4][2] = "O";
            return board[4][2];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
     else if (("A3".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[0][4])) {
            board[0][4] = "O";
            return board[0][4];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("B3".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[2][4])) {
            board[2][4] = "O";
            return board[2][4];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("C3".equalsIgnoreCase(position)) && (player1turn == true)) {
        if (" ".equals(board[4][4])) {
            board[4][4] = "O";
            return board[4][4];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("A1".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[0][0])) {
            board[0][0] = "X";
            return board[0][0];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("B1".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[2][0])) {
            board[2][0] = "X";
            return board[2][0];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("C1".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[4][0])) {
            board[4][0] = "X";
            return board[4][0];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("A2".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[0][2])) {
            board[0][2] = "X";
            return board[0][2];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("B2".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[2][2])) {
            board[2][2] = "X";
            return board[2][2];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("C2".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[4][2])) {
            board[4][2] = "X";
            return board[4][2];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
     else if (("A3".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[0][4])) {
            board[0][4] = "X";
            return board[0][4];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("B3".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[2][5])) {
            board[2][4] = "X";
            return board[2][4];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else if (("C3".equalsIgnoreCase(position)) && (player1turn == false)) {
        if (" ".equals(board[4][4])) {
            board[4][4] = "X";
            return board[4][4];
        } else {
            System.out.println("That spot is taken please choose another place");
            return "retry";
        }
    }
    else {
        if ((position == null) || ("help".equalsIgnoreCase(position)) || ("turn".equalsIgnoreCase(position)) || ("board".equalsIgnoreCase(position))) {
            return "retry";
        } else {
            System.out.println("Error please enter a valid position found by entering \"help\" ");
            return "retry";
        }
    }
}
}


据我所知,问题涉及我的长度changePosition()方法。如果我在控制台“ A1”中输入,结果将是:

Player 1, noughts turn. Please choose a position.
A1
That spot is taken please choose another place
retry
That spot is taken please choose another place
board

O| |
-----
 | |
-----
 | |


这使我感到困惑,因为似乎if块实际上已触发并导致board [0] [0]被更改,但是由于某种原因必须移至else块并将消息打印到控制台并返回“重试”。

我尝试将if块内的返回值更改为标准字符串,例如“ success”,但这似乎无法解决问题。另外,当我将鼠标悬停在返回类型上时,IDE会将其标识为返回点。我确信(我有信心)使用的语法正确。

我希望这是我冗长的代码存在的问题,我相信这对你们所有人来说都是戈尔。

谢谢。

最佳答案

问题是您在此循环的每次迭代中多次调用changePosition方法:

while ("retry".equals(changePosition(position, player1turn, board))) {
        Scanner keyboard = new Scanner (System.in);
        position = keyboard.nextLine();

        if ("help".equalsIgnoreCase(position)) {
            commands();
        }
        else if ("turn".equalsIgnoreCase(position)){
            turn(player1turn);
        }
        else if ("board".equalsIgnoreCase(position)){
            printBoard(board);
        }
        else {
             if (!"retry".equalsIgnoreCase(changePosition(position, player1turn, board))) {
                 System.out.println(changePosition(position, player1turn, board));
                 if (player1turn == true) {
                     player1turn = false;
                 }
                 else if (player1turn == false) {
                     player1turn = true;
                 }
             }
        }
    }
}


首先,它在while循环的条件下被调用:


  while(“ retry” .equals(changePosition(position,player1turn,board)))


但是由于参数位置一开始仍然为空,因此它仅会返回"retry"并在首次发生时进入循环。

输入职位后,请在此处调用方法:


  如果(!“ retry” .equalsIgnoreCase(changePosition(position,player1turn,
  板)))


如果现货是免费的,它将正常工作并且不返回"retry",因此输入if语句块。这就是您的情况,并且从那一刻开始实际填充A1。

然后,您立即在下一行再次调用该方法:


  System.out.println(changePosition(position,player1turn,board));


但是由于现在A1已被占用,这一次将输出错误“该地点已被占用,请选择另一个地方”并返回您也打印的"retry"错误。

然后在下一次迭代的开始再次检查循环条件时,再次调用该方法。并且由于您现在使用相同的参数第三次调用它,它将再次输出错误并返回"retry"作为返回值,从而再次进入循环。

解决方案是每次迭代仅调用一次函数,然后保存返回值并进行处理。
您还应该使用do-while循环,因为它始终应该至少运行一次:

String changePositionResult = null;
do {
    final Scanner keyboard = new Scanner(System.in);
    position = keyboard.nextLine();

    if ("help".equalsIgnoreCase(position)) {
        commands();
    } else if ("turn".equalsIgnoreCase(position)) {
        turn(player1turn);
    } else if ("board".equalsIgnoreCase(position)) {
        printBoard(board);
    } else {
        changePositionResult = changePosition(position, player1turn, board);
        if (!"retry".equalsIgnoreCase(changePositionResult)) {
            System.out.println(changePositionResult);
            if (player1turn == true) {
                player1turn = false;
            } else if (player1turn == false) {
                player1turn = true;
            }
        }
    }
} while ("retry".equals(changePositionResult));

关于java - 如果满足条件并激活了条件,则阻止打印,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48585418/

10-10 21:49