我已经创建了一个多维数组(identifier [] []),我将其分配给变量'x',以识别具有整数1-9的单元格,因此我可以更舒适地使用它们。但是,我怎样才能将'x'值分配到cell []数组中,所以我可以将它传递给我的main函数并在for循环之后将其打印出来(“Cell number are:”)?如果我需要更改我的printTable函数,那么如何更改它,所以返回值将是一个数组? (我正在尝试制作一个井字游戏程序)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
printTable();
System.out.print("Cell numbers are: ");
for(int i = 0; i < 9; i++) {
System.out.print("");
if (i != 8) {
System.out.print(", ");
} else {
System.out.print(".");
}
}
input.close();
} // End of main.
public static void printTable() {
int rows = 3;
int columns = 3;
int[][] identifier = new int[rows][columns];
int x = 1;
int[] cell = new int[9];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
identifier[i][j] = x;
if (i == 0 && j == 0) {
System.out.println("+---+---+---+");
}
System.out.print("| " + x + " ");
cell[x];
x++;
if (j == columns - 1) {
System.out.print("|");
}
}
System.out.println("");
System.out.println("+---+---+---+");
}
System.out.println("Enter a number between (1-9): ");
} // End of printTable.
答案 0 :(得分:0)
你的表(数组)必须是全局变量。 我建议你开始创建一个类表,并从tic-tac-toe的基本概念开始,比如新表,清除表,show table等。
互联网上有很多tutoriais,只需选择一个!祝你好运,如果你有疑问,请告诉我们。
[编辑] 这看起来是一个很好的例子。 http://www.progressivejava.net/2012/11/How-to-make-a-Tic-Tac-Toe-game-in-Java.html
答案 1 :(得分:0)
不是很好的方法,但要解决您需要的特定问题
从printTable函数返回结果:
public static void printTable()
更改为public static int[] printTable()
在printTable函数的末尾添加return cell;
在主要功能中将printTable();
更改为int[] cell2 = printTable();
并更改“for循环”:
for(int i = 0; i < 9; i++) { System.out.print("");
更改为for(int i = 0; i < 9; i++) { System.out.print(cell2[i]);