Tic Tac Toe在Java中打印错误(Tic Tac Toe board printing incorrectly in Java)

编程入门 行业动态 更新时间:2024-10-27 05:33:12
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

| _ | _ | _ | | _ | _ | _ | | _ | _ | _ |

更多推荐

本文发布于:2023-08-04 03:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405849.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Toe   Tic   Tac   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!