[英]Tic Tac Toe board printing incorrectly in Java
我正在用Java創建井字游戲,要做的第一步是制作棋盤。
由於存在環路,該板應該為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;
}
}
感謝您的時間!
嘗試這個:
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
給出輸出
| _ | _ | _ |
| _ | _ | _ |
| _ | _ | _ |
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.