gpt4 book ai didi

java - Tic Tac Toe 棋盘 - Java

转载 作者:行者123 更新时间:2023-11-30 06:09:27 27 4
gpt4 key购买 nike

我试图为 Tic Tac Toe 游戏编写代码。我编写了以下代码来显示游戏板,但出现了问题,并且没有显示所需的输出。你能帮我找出错误在哪里吗?

这里:

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}};

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");
}
}
}

输出:

Image of the output

最佳答案

您忘记了 switch 语句中的 break;,请尝试:

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;
}
}

Read here for why we need break;

关于java - Tic Tac Toe 棋盘 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37794327/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com