在此代码中,我尝试创建一个代表tic tac脚趾板(带用户输入)的二维数组,但无论我在“TicTacLine “,该节目总是提出”你以前从来没有玩过井字趾吗?没关系,如果你没有,但只是供参考,它使用x和o“。这是我写的信息,如果字符在uno, dos和tres不是x或o的。通过使用二维数组创建一个tic tac脚趾板
public class TicTacToe {
public static void main(String[] args) {
int TicTac[][]= new int[3][3];
System.out.println("Enter the Tic Tac Toe board you want to see, one line at a time.");
Scanner scanner = new Scanner(System.in);
String TicTacLine = scanner.nextLine();
int loop = 0;
if (TicTacLine.length()<3 | TicTacLine.length()>3) { // I try to define the array by a series of inputs that go in the while loop.
System.out.println("Tic-tac-toe plays in a 3×3 grid. This means if you want to input a line, you would want to input 3 characters, no more, no less.");
} else {
while (loop != 3) { // we count the loops so that there's only 3 different lines
char uno = TicTacLine.charAt(0);
char dos = TicTacLine.charAt(1);
char tres = TicTacLine.charAt(2);
if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o') {
System.out.println("Have you never played Tic Tac Toe before ? It's okay if you haven't, but just FYI, it plays with x's and o's.");
break;
} else {
if (loop == 0) {
TicTac[0][0] = uno;
TicTac[0][1] = dos;
TicTac[0][2] = tres;
loop = ++loop;
TicTacLine = scanner.nextLine();
} if (loop == 1) {
TicTac[1][0] = uno;
TicTac[1][1] = dos;
TicTac[1][2] = tres;
loop = ++loop;
TicTacLine = scanner.nextLine();
} if (loop == 2) {
TicTac[2][0] = uno;
TicTac[2][1] = dos;
TicTac[2][2] = tres;
loop = ++loop;
TicTacLine = scanner.nextLine();
}
}
}
}
if (loop == 3) {
for(int[] row : TicTac) {
PrintingRow(row);
}
}
}
}
请确保您使用.equals而不是==来比较字符串。另外,您正在使用|而不是||为“或”。此外,在“TicTacLine.length()<3 | TicTacLine.length()> 3”行中,您可以改为使用“TicTacLine.length()!= 3”。另外,在java中你应该使用一个约定:你在java中命名了一个变量“TicTacLine”,我们使用lowerCamelCase,所以它应该重命名为:“ticTacLine”你的类名“TicTacToe”很好,类名使用PascalCase 。编辑:你正在比较字符的正确方法。我认为这些是一秒钟的字符串。 – retodaredevil
想一想:'uno'的价值会让'uno!='x'| uno!='o''是'false'?它需要是&&'。 – 4castle
我确实改变了我的变量和ticTacLine.length()!= 3的名字,但它并没有让错误消息消失。 – Dracose