(Java) 初始化后打印 Tic Tac Toe Board
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10522430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(Java) Printing Tic Tac Toe Board after Initialization
提问by Huy
I'm creating a simple Tic Tac Toe game. I've declared my 3 by 3 board and initialized each play area to ' '
or a space character.
我正在创建一个简单的井字游戏。我已经声明了我的 3 x 3 板并将每个游戏区初始化为' '
一个空格字符。
However, when I try to print the board so that I get something that looks like this:
但是,当我尝试打印板以得到如下所示的内容时:
1 2 3
A | |
----------
B | |
----------
C | |
Nothing gets printed.
什么都不会打印。
Here is my TicTacToe.java:
这是我的 TicTacToe.java:
import java.util.Scanner;
public class TicTacToe{
public static void main(String[] args) {
new TicTacToe();
}
private char[][] board;
private char player;
public TicTacToe() {
for(int i = 0; i < 3; i++)
{
for(int j = 0; j <3; j++)
{
board[i][j] = ' ';
}
}
player = 'X';
System.out.println(" 1 2 3");
System.out.println("A" + board[0][0] + "|" + board[0][1] + "|" + board[0][2]);
System.out.println("-----");
System.out.println("B" + board[1][0] + "|" + board[1][1] + "|" + board[1][2]);
System.out.println("-----");
System.out.println("C" + board[2][0] + "|" + board[2][1] + "|" + board[2][2]);
}
I've read from Murach's 4th edition that when the class is executed, the constructor is executed as well so I assume that the print functions will be executed.
我从 Murach 的第 4 版中读到,当类被执行时,构造函数也会被执行,所以我假设将执行打印函数。
Question:
问题:
How do I print my tic tac toe board like the one above to my console?
如何将我的 tic tac toe 板像上面一样打印到我的控制台?
Edit: Thank you for the help. Turns out I had to call the constructor instead of it automatically being executed. Code above does not contain the solution.
编辑:谢谢你的帮助。结果我不得不调用构造函数而不是自动执行它。上面的代码不包含解决方案。
回答by Jeffrey
When a class is executed, its main
method is executed. Your main
method is empty, so nothing is happening. If you want to call the constructor, you have to explicitly do so.
当一个类被执行时,它的main
方法就会被执行。你的main
方法是空的,所以什么都没有发生。如果要调用构造函数,则必须显式这样做。
public static void main (String[] args) {
new TicTacToe();
}
回答by ametren
You don't actually have anything in your main method
您的主要方法中实际上没有任何内容
import java.util.Scanner;
public class TicTacToe{
public static void main(String[] args) {
// insert code to set up your tictactoe object here
TicTacToe ttt = new TicTacToe();
}
private char[][] board;
private char player; // 'X' or 'O'
public TicTacToe() {
for(int i = 0; i < 3; i++)
{
for(int j = 0; j <3; j++)
{
board[i][j] = ' ';
}
}
player = 'X';
System.out.println(" 1 2 3");
System.out.println("A" + board[0][0] + "|" + board[0][1] + "|" + board[0][2]);
System.out.println("-----");
System.out.println("B" + board[1][0] + "|" + board[1][1] + "|" + board[1][2]);
System.out.println("-----");
System.out.println("C" + board[2][0] + "|" + board[2][1] + "|" + board[2][2]);
}
回答by Bhavik Ambani
I have made some changes in the code
我对代码做了一些改动
public class TicTacToe {
public static void main(String[] args) {
new TicTacToe();
}
private char[][] board = new char[3][3];
private char player;
public TicTacToe() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
player = 'X';
System.out.println(" 1 2 3");
System.out.println("A" + board[0][0] + "|" + board[0][1] + "|"
+ board[0][2]);
System.out.println("-----");
System.out.println("B" + board[1][0] + "|" + board[1][1] + "|"
+ board[1][2]);
System.out.println("-----");
System.out.println("C" + board[2][0] + "|" + board[2][1] + "|"
+ board[2][2]);
}
}
Hope this will help you.
希望这会帮助你。
回答by Makoto
Nothing's printing because you're not instantiating anything inside of main()
. You have to use your objects inside of main()
before anything will happen.
什么都没有打印,因为您没有在main()
. main()
在任何事情发生之前,你必须在里面使用你的对象。