如果满足并激活了条件,则不进行雕版印刷 我理解这是一个常见问题,我在C++和Python上看到过,但似乎找不到解释来解决java问题。
附件是我的程序代码,相当长,因为我还是一个新手,正在学习如何简化它如果满足并激活了条件,则不进行雕版印刷 我理解这是一个常见问题,我在C++和Python上看到过,但似乎找不到解释来解决java问题。,java,Java,附件是我的程序代码,相当长,因为我还是一个新手,正在学习如何简化它 import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { boolean gameActive = true; boolean player1turn = true; String board[][]= {{" ","|"," ","|"," "},{"-","-","-
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";
}
}
}
}
这让我感到困惑,因为似乎if块实际上被触发,并导致线路板[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(“重试”。等于(更改位置(位置、播放者1转、棋盘)))
但是,由于参数位置在开始时仍然为空,因此它将简单地返回“retry”
,并在第一次出现时进入循环
输入职位后,在此处调用该方法:
如果(!“重试”).equalsIgnoreCase(改变位置)(位置,播放器1转,
(董事会)
如果spot是空闲的,它将工作并且不返回“retry”,因此进入if
语句块。这就是你的情况,从那一刻起,A1就被填满了
然后在下一行中立即再次调用该方法:
系统输出打印LN(改变位置(位置、播放器1转、板))
但由于现在A1已被占用,因此这一次将输出错误“该点已被占用,请选择其他位置”,并返回“重试”
,您也将打印该错误
然后,在下一次迭代开始时再次检查循环条件时,再次调用该方法。由于您现在使用相同的参数第三次调用它,它将再次输出错误并返回“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));
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));