在Java中,如何使用特定数量的随机值初始化2d数组。
我正在做一个模拟游戏,里面有涂鸦虫和蚂蚁,随着模拟的进行,涂鸦虫试图吃掉蚂蚁。我遇到的问题是初始化我创建的2d数组。我需要100只蚂蚁和5只涂鸦虫随机放在网格上。我把网格随机化了,但是作为一个整体,我得到了随机数量的蚂蚁和涂鸦虫。我也在使用一个更小的数组,直到我能工作为止。任何帮助都将不胜感激在Java中,如何使用特定数量的随机值初始化2d数组。,java,arrays,random,2d,Java,Arrays,Random,2d,我正在做一个模拟游戏,里面有涂鸦虫和蚂蚁,随着模拟的进行,涂鸦虫试图吃掉蚂蚁。我遇到的问题是初始化我创建的2d数组。我需要100只蚂蚁和5只涂鸦虫随机放在网格上。我把网格随机化了,但是作为一个整体,我得到了随机数量的蚂蚁和涂鸦虫。我也在使用一个更小的数组,直到我能工作为止。任何帮助都将不胜感激 Random rand = new Random(); int[][] cells = new int[10][10]; public void display() { for(int i
Random rand = new Random();
int[][] cells = new int[10][10];
public void display() {
for(int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
cells[i][j] = (int) (Math.random()*3);
if(cells[i][j] == 2) { // 2 = ants;
cells[i][j] = 4;
}
if(cells[i][j] == 1) { // 1 = doodlebugs
cells[i][j] = 3;
}
if(cells[i][j] == 0) {
cells[i][j] = 0;
}
System.out.print(cells[i][j]);
}
System.out.println();
}
}
Random rand=new Random();
int[][]单元格=新int[10][10];
公共空间显示(){
for(int i=0;i一个简单的方法是创建一个for循环,循环次数与您想要的最大数量相同(在本例中为ants和doodlebugs)。在for循环的每次迭代中,您可以为该对象生成随机坐标。这相当于两个for循环,一个用于ants,一个用于doodlebugs
for (int i = 0; i < desiredNumOfAnts; i++)
{
int randX = rand.nextInt(cells[i].length); // this generates a random X coordinate, up to the length of the current row
int randY = rand.nextInt(cells.length); // this generates a random Y coordinate, according to the height of the array
/* using these coordinates, insert a new ant into the cells */
}
for(int i=0;i
看看你是否能自己解决剩下的问题!你创建了一个10x10阵列,这意味着它有100个位置,这意味着所有位置都应该被蚂蚁占据?(因为你告诉你需要100只蚂蚁-除非同一位置可以有多个昆虫)
无论如何,我认为你在做一个“反向”逻辑。想想什么应该是随机的,什么不应该是随机的。你正在循环整个数组并调用random()
,以了解每个位置必须放置什么昆虫,因此你无法控制每个昆虫将被创建多少
如果你需要确切数量的蚂蚁和涂鸦虫,它们是固定的-你不需要调用random()
来知道它是蚂蚁还是涂鸦虫,你已经知道你需要多少。必须随机的是它们的位置,所以你应该调用random()
获取行和列位置,而不是昆虫类型
首先,我将创建一些类来表示昆虫和细胞:
// enum type, better than "magic numbers"
public enum Insect {
ANT,
DOODLEBUG;
}
public class Cell {
// the insect placed in this cell
private Insect insect;
public Cell() {
// cell starts without any insect
this.insect = null;
}
public Insect getInsect() {
return insect;
}
public void setInsect(Insect insect) {
this.insect = insect;
}
// check if cell already has an insect
public boolean isOccupied() {
return this.insect != null;
}
}
然后我初始化电路板:
int size = 10; // assuming a square
Cell[][] cells = new Cell[size][size];
// fill with empty cells
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
cells[i][j] = new Cell();
}
}
int size=10;//假设为正方形
单元格[][]单元格=新单元格[大小][大小];
//用空单元格填充
对于(int i=0;i
然后我用昆虫填满它:
// how many ants? Just an example, change the value according to your needs
int nAnts = 8;
// how many doodlebugs? Just an example, change the value according to your needs
int nBugs = 2;
Random rand = new Random();
// place the ants
for (int i = 0; i < nAnts; i++) {
int row, column;
// get a random position that's not occupied
do {
row = rand.nextInt(size);
column = rand.nextInt(size);
} while (cells[row][column].isOccupied());
// fill with ant
cells[row][column].setInsect(Insect.ANT);
}
// place the doodlebugs
for (int i = 0; i < nBugs; i++) {
int row, column;
// get a random position that's not occupied
do {
row = rand.nextInt(size);
column = rand.nextInt(size);
} while (cells[row][column].isOccupied());
// fill with doodlebug
cells[row][column].setInsect(Insect.DOODLEBUG);
}
//有多少只蚂蚁?举个例子,根据需要更改值
int-nAnts=8;
//有多少涂鸦虫?举个例子,根据需要更改值
int-nBugs=2;
Random rand=新的Random();
//安置蚂蚁
for(int i=0;i
用于放置昆虫的循环有点重复,因此您也可以将它们重构为一种方法
<>我也假设<代码>细胞< /C>是一个正方形,但是如果不是,只要为行和列的数目创建2个变量,并且相应地改变代码,虽然逻辑是相同的。我已经随机化了网格,但是作为一个整体——你是什么意思?考虑2次迭代:第一次迭代随机地用蚂蚁填充网格。第二次迭代会用涂鸦错误随机填充网格。第一次迭代是0…99,而第二次是0…4。在使用该单元格之前,双方都要检查单元格以确保其为空(0)。这更有意义。我只是没能这样查看它。谢谢!