Tic Tac Toe在Java中打印错误(Tic Tac Toe board printing incorrectly in Java)
我正在用Java创建一个tic-tac-toe游戏,我做的第一步就是制作棋盘。
由于循环,该板应该是3x3。 谁能帮我吗? 这是代码:
package myProject; import java.util.Scanner; public class TicTacDoe { public static int row,col; public static Scanner scan = new Scanner(System.in); public static char[][] board = new char[3][3]; public static char turn = 'X'; public static void main(String[] args) { for(int i = 0;i < 3; i++) { for(int j = 0;j < 3;j++){ board[i][j] = '_'; } PrintBoard(); } } public static void Play() { } public static void PrintBoard() { for(int i = 0;i < 3; i++) { System.out.println(); for(int j = 0;j < 3;j++) { if(j==0) System.out.println("| "); System.out.print(board[i][j] + " | "); } } } public boolean GameOver(int rMove, int cMove) { return false; } }感谢您的时间!
I am creating a tic-tac-toe game in Java and the first step I do is make the board.
The board is supposed to be 3x3 because of the loops. Can anyone help me out? Here is the code:
package myProject; import java.util.Scanner; public class TicTacDoe { public static int row,col; public static Scanner scan = new Scanner(System.in); public static char[][] board = new char[3][3]; public static char turn = 'X'; public static void main(String[] args) { for(int i = 0;i < 3; i++) { for(int j = 0;j < 3;j++){ board[i][j] = '_'; } PrintBoard(); } } public static void Play() { } public static void PrintBoard() { for(int i = 0;i < 3; i++) { System.out.println(); for(int j = 0;j < 3;j++) { if(j==0) System.out.println("| "); System.out.print(board[i][j] + " | "); } } } public boolean GameOver(int rMove, int cMove) { return false; } }Thank you for your time!
最满意答案
尝试这个:
import java.util.Scanner; public class TicTacToe { public static int row,col; public static Scanner scan = new Scanner(System.in); public static char[][] board = new char[3][3]; public static char turn = 'X'; public static void main(String[] args) { for(int i = 0;i < 3; i++) { for(int j = 0;j < 3;j++){ board[i][j] = '_'; } } PrintBoard(); } public static void Play() { } public static void PrintBoard() { for(int i = 0;i < 3; i++) { System.out.println(); for(int j = 0;j < 3;j++) { if(j==0) System.out.print("| "); System.out.print(board[i][j] + " | "); } } } public boolean GameOver(int rMove, int cMove) { return false; } }将PrintBoard()移出嵌套的for循环,并将if(j == 0)更改为System.out.print
给出输出
| _ | _ | _ | | _ | _ | _ | | _ | _ | _ |try this:
import java.util.Scanner; public class TicTacToe { public static int row,col; public static Scanner scan = new Scanner(System.in); public static char[][] board = new char[3][3]; public static char turn = 'X'; public static void main(String[] args) { for(int i = 0;i < 3; i++) { for(int j = 0;j < 3;j++){ board[i][j] = '_'; } } PrintBoard(); } public static void Play() { } public static void PrintBoard() { for(int i = 0;i < 3; i++) { System.out.println(); for(int j = 0;j < 3;j++) { if(j==0) System.out.print("| "); System.out.print(board[i][j] + " | "); } } } public boolean GameOver(int rMove, int cMove) { return false; } }Moves the PrintBoard() out of the nested for loop, and changed the if(j==0) to System.out.print
Gives the output
| _ | _ | _ | | _ | _ | _ | | _ | _ | _ |更多推荐
发布评论