我正在尝试在一个方法中创建一个数组(或者函数?或者对象?问题 - 所有这些单词之间有什么区别?)然后在另一个方法中使用它的长度(我将在其中使用它其他地方)。我的老师告诉我,我不必返回数组,因为我只是修改了位置,所以阵列没有被破坏或什么的。我会在main中声明它然后在得到大小输入后我无法调整大小(我不认为?)。
是否有人关注此事?
public class Update {
public static void main(String[] args) {
System.out.println("This program will simulate the game of Life.");
createMatrix();
// birthAndLive();
printMatrix();
}
public static void createMatrix() {
Scanner console = new Scanner(System.in);
System.out.println("Please input the size of your board.");
System.out.println("Rows:");
final int rows = console.nextInt();
System.out.println("Columns:");
final int columns = console.nextInt();
System.out.println("Please enter a seed:");
final long seed = console.nextLong();
boolean[][] board = new boolean[rows][columns];
Random seedBool = new Random(seed);
}
public static void printMatrix() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == false)
System.out.print(" - ");
else
System.out.print(" # ");
}
System.out.println();
}
}
答案 0 :(得分:3)
您可以通过将board
传递给您的打印功能来解决此问题。
class Update {
public static void main(String[] args) {
System.out.println("This program will simulate the game of Life.");
createMatrix();
// birthAndLive();
printMatrix();
}
public static void createMatrix() {
Scanner console = new Scanner(System.in);
System.out.println("Please input the size of your board.");
System.out.println("Rows:");
final int rows = console.nextInt();
System.out.println("Columns:");
final int columns = console.nextInt();
System.out.println("Please enter a seed:");
final long seed = console.nextLong();
boolean[][] board = new boolean[rows][columns];
Random seedBool = new Random(seed);
printMatrix(board);
}
public static void printMatrix(boolean[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == false)
System.out.print(" - ");
else
System.out.print(" # ");
}
System.out.println();
}
}
}
我不知道你的老师允许你改变多少代码。如果需要从main
调用所有函数,那么您必须将数组创建代码放在main函数中,否则您将不得不求助于return语句或类变量。