Java二维布尔数组
我的家庭作业是创造一个康威的生活游戏(不是那么)简单的娱乐。确切说明:Java二维布尔数组,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我的家庭作业是创造一个康威的生活游戏(不是那么)简单的娱乐。确切说明: 提示用户输入(i,j)对(均为非负整数)的列表(当为任一i读取负整数时停止 (或j) 提示用户输入时间步数 根据用户输入的(i,j)对初始化网格 显示网格的初始状态(调用displayGrid()方法) 对于每个时间步,根据Conway的规则更新网格(调用updateGrid()方法)并显示网格(调用 displayGrid()方法) 我的程序输出现在已更改: Please enter list of (i, j) pair
displayGrid()
方法)updateGrid()
方法)并显示网格(调用
displayGrid()
方法)Please enter list of (i, j) pairs for populated cells(negative i or j to quit):
6 4 6 5 6 6 6 7 6 8
-1 -1
Enter number of time steps:
2
Initial Grid:
######
Time step 1
Time step 2
我的新问题是什么让我的代码“杀死”一切?为什么没有任何“真实”的
这是我的密码:
java.util.Scanner;
class P8{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean[][] multi = new boolean[10][10];
int i, j, N, x, y, h;
for(x=1; x<multi.length; x++){
for(y=1; y<multi.length; y++){
multi[x][y] = false;
}
}
System.out.println("Please enter list of (i, j) pairs for populated cells(negative i or j to quit):");
while(true){
i = in.nextInt();
j = in.nextInt();
if(i <= 0 || j <= 0){
break;}
multi[i][j] = true;
}
System.out.println("Enter number of time steps:");
N = in.nextInt();
System.out.println("Initial Grid: \n");
displayGrid(multi);
for(i = 1; i<N+1; i++){
System.out.printf("Time step %d\n", i);
updateGrid(multi);
displayGrid(multi);
System.out.println("\n");
}
}
/******************************************
void updateGrid(boolean mat[][]); updates
the 2 dimensional array using Conway's
standard rules. Does this by calling
"neighbors" function
******************************************/
public static void updateGrid(boolean mat[][]){
boolean[][] temp = new boolean[mat.length][mat.length];
int i, j, row, col, n=0;
for(row = 0; row < mat.length; row++){
for(col = 0; col < mat.length; col++){
neighbors(mat, row, col);
if(n>=4 || n<=1)
mat[row][col] = false;
else
mat[row][col] = true;
}
}
}
/******************************************
void neighbors(boolean mat[][]int row, int col)
checks how many "neighbors" are around a given point
******************************************/
public static int neighbors(boolean mat[][], int row, int col){
int N =0;
if((row-1 >= 0)&&(col-1 >= 0))
N = mat[row-1][col-1] ? N + 1 : N;
if((row >=0)&&(col-1 >= 0))
N = mat[row][col-1] ? N+1 : N;
if((row+1 < mat.length)&&(col-1 >= 0))
N = mat[row+1][col-1] ? N+1 : N;
if((row+1 < mat.length)&&(col < mat[0].length))
N = mat[row+1][col] ? N+1 : N;
if((row+1 < mat.length)&&(col+1 < mat[0].length))
N = mat[row+1][col+1] ? N+1 : N;
if((row < mat.length)&&(col+1 < mat[0].length))
N = mat[row][col+1] ? N+1 : N;
if((row-1 >= 0)&&(col+1 < mat[0].length))
N = mat[row-1][col+1] ? N+1 : N;
if((row-1 >= 0)&&(col < mat[0].length))
N = mat[row-1][col] ? N+1 : N;
return N;
}
/******************************************
void displayGrid(int mat[][]) prints out
a two dimensional array
******************************************/
public static void displayGrid(boolean mat[][]){
int x, y;
for(x=1; x<mat.length; x++){
for(y = 1; y<mat.length; y++){
if(mat[x][y])
System.out.printf("#");
else
System.out.printf(" ");
}
System.out.println();
}
}
}
java.util.Scanner;
P8类{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
布尔[][]多=新布尔[10][10];
int i,j,N,x,y,h;
对于(x=1;x
我的问题是,在我的代码中,multi[][]在哪里变成了“true”,因为这是我必须打印的标准“#”
在displayGrid
方法中
如果(mat[x][y]=true)
是赋值,而不是比较
使用
相反
您永远不需要编写bool==true
(或bool==false
),因为您可以将它们编写为bool
(和!bool
)-后一种形式避免了遗漏一个=
符号和更改表达式语义的可能性。如果则否则没有意义。您可以使用else
,因为您只是颠倒了原始条件。我考虑了您的意见并更新了我的代码。:D尽管有一个新的pr这个问题已经浮出水面,我相信它在我的邻居()函数中,但我似乎找不到它。啊,好吧,我明白你们的意思。我会改进我的代码。
if (mat[x][y])