[英]Tic Tac Toe Board - 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}};
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");
}
}
}
輸出:
你忘了break;
在您的switch語句中,嘗試:
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;
}
}
您需要休息一下(也許是制表符才能在標志中獲得相同的距離)
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.