Java Tic Tac趾板-爪哇
我正试着为井字游戏写一个代码。我编写了下面的代码来显示游戏的棋盘,但是出现了一些问题,它没有显示所需的输出。你能帮我找出错误在哪里吗 在这里: 0表示空白 1代表X和 2表示OJava Tic Tac趾板-爪哇,java,Java,我正试着为井字游戏写一个代码。我编写了下面的代码来显示游戏的棋盘,但是出现了一些问题,它没有显示所需的输出。你能帮我找出错误在哪里吗 在这里: 0表示空白 1代表X和 2表示O public class Trying { public static void main(String[] args) { int board[][] = {{1,0,2}, {0,1,0}, {0,2,0}};
public class Trying
{
public static void main(String[] args) {
int board[][] = {{1,0,2},
{0,1,0},
{0,2,0}};
for(int row = 0; row<3; row++)
{
for(int col = 0; col<3; col++)
{
printCell(board[row][col]);
if(col<2)
{
System.out.print(" | ");
}
}
System.out.println("\n------------");
}
}
public static void printCell(int content){
switch(content){
case 0: System.out.print(" ");
case 1: System.out.print("X");
case 2: System.out.print("O");
}
}
}
公共类正在尝试
{
公共静态void main(字符串[]args){
int板[][]={{1,0,2},
{0,1,0},
{0,2,0}};
对于(int row=0;row您忘记了switch语句中的break;
s),请尝试:
public static void printCell(int content){
switch(content){
case 0: System.out.print(" ");
break;
case 1: System.out.print("X");
break;
case 2: System.out.print("O");
break;
}
}
您需要一个中断(可能需要一个制表符来获得符号中相同的距离)
publicstaticvoidmain(字符串[]args){
int board[][]={{1,0,2},{0,1,0},{0,2,0};
对于(int行=0;行<3;行++){
for(int col=0;col<3;col++){
打印单元(板[行][列]);
if(col<2){
系统输出打印(“|”);
}
}
}
System.out.println(“\n--------------------------------------------”);
}
公共静态void打印单元(int内容){
交换机(内容){
案例0:
系统输出打印(“\t\t”);
打破
案例1:
系统输出打印(“\tX\t”);
打破
案例2:
System.out.print(“\tO\t”);
打破
}
}
很遗憾,你刚刚删除了关于偶数的C++问题……无论如何,这是你的解决方案:
public static void main(String[] args) {
int board[][] = { { 1, 0, 2 }, { 0, 1, 0 }, { 0, 2, 0 } };
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
printCell(board[row][col]);
if (col < 2) {
System.out.print("|");
}
}
}
System.out.println("\n--------------------------------------------");
}
public static void printCell(int content) {
switch (content) {
case 0:
System.out.print("\t \t");
break;
case 1:
System.out.print("\tX\t");
break;
case 2:
System.out.print("\tO\t");
break;
}
}