2016-06-13 7 views
2

私はTic Tac Toe Gameのコードを書こうとしていました。私はゲームのボードを表示するために次のコードを書いたが、何かが間違っていて、必要な出力を表示していない。間違いがどこにあるのか理解してもらえますか?ここ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"); 
     } 
    } 
} 

出力:

Image of the output

+0

あなただけの偶数番号についてのあなたのC++質問を削除したことは本当に悲しいです。 。 。とにかくここにあなたの解決策があります:https://pastebin.com/jdTAwaG4 –

答えて

4

あなたはしてみてください、あなたの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;

1

あなたがブレーク(そしておそらく必要tabulatorの文字は同じdisを得るために看板でタンス)

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

enter image description here