Java 随机数只初始化一次
我试图给2d数组迷宫中每个对象“房间”的每个索引一个0到9之间的随机值。但是,在for循环中,我创建了每个唯一房间的编号,它将为所有房间提供为最后一个房间生成的编号。我完全被难住了 我尝试将随机化分配给一种新方法,并使用“迷宫[I][j].structure[1][2]直接更改索引,但没有效果Java 随机数只初始化一次,java,Java,我试图给2d数组迷宫中每个对象“房间”的每个索引一个0到9之间的随机值。但是,在for循环中,我创建了每个唯一房间的编号,它将为所有房间提供为最后一个房间生成的编号。我完全被难住了 我尝试将随机化分配给一种新方法,并使用“迷宫[I][j].structure[1][2]直接更改索引,但没有效果 public class Room { static int[][] structure; boolean exists; // Constructor Declaration
public class Room {
static int[][] structure;
boolean exists;
// Constructor Declaration of Class
public Room(int[][] structure, boolean exists)
{
this.structure = structure;
this.exists = exists;
}
public static void main(String[] args) {
Room[][] maze = new Room[3][3];
// setup array of rooms
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
int a = (int)(Math.random()*9);
int b = (int)(Math.random()*9);
int c = (int)(Math.random()*9);
Out.println(a + ", " + b + ", " + c);
int[][] roomBuild = {{1,1,1,1,1},
{1,a,b,c,1},
{1,1,1,1,1}};
maze[i][j] = new Room(roomBuild, true);
//int[] nums = Logic.genRoom();
//maze[i][2].structure[1][1] = nums[0];
//maze[i][2].structure[1][2] = nums[1];
//maze[i][2].structure[1][3] = nums[2];
}
}
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
for (int k=0; k<3; k++) {
for (int l=0; l<5; l++) {
System.out.print(maze[i][j].structure[k][l]);
}
Out.println();
}
Out.println(i + "," + j + ", " + maze[i][j].exists);
Out.println();
}
}
}
}
公共教室{
静态int[][]结构;
布尔存在;
//类的构造函数声明
公共房间(int[][]结构,存在布尔值)
{
这个结构=结构;
this.exists=存在;
}
公共静态void main(字符串[]args){
房间[]迷宫=新房间[3][3];
//设置房间阵列
对于(int i=0;i您将结构声明为静态
——这意味着只有一个,您会不断覆盖它
将其声明为您的存在
变量,例如,非静态。为什么不使用?