我想编写一个在循环中生成特定数字的代码。
例如生成如下数字:
column 1 - row 1
column 1 - row 2
column 1 - row 3
然后生成此(例如):
column 2 - row 1
column 2 - row 2
column 2 - row 3
column 2 - row 4
有没有办法编写这个算法?非常感谢你
代码,我有这样的代码,但我想管理它:
boolean[][] mapEq = new boolean[mapWidth][mapHeight];
int free = mapWidth * mapHeight;
int randomFree = ((int) (free * Math.random()));
int x = 0, y = 0;
for (int i = 0, k = 0; i < mapWidth; i++) {
for (int j = 0; j < mapHeight; j++) {
if (!mapEq[i][j]) {
if (k == randomFree) {
x = i;
y = j;
break SearchRandom;
} else {
k++;
}
}
}
}
它创建了一个随机的东西,但我想制作一个特定的数字,例如
第1列的- 第1行,第2行和第3行
和第2列 - 第1行和第2行以及第3行和第4行
答案 0 :(得分:0)
也许是这样的?
int[] columnLengths = {3, 4}; // Lengths of the columns
for(int i = 0; i < columnLengths.length; i++) { // Loop the columns
for(int j = 0; j < columnLengths[j]; j++) { // Loop the rows
System.out.println("column " + i + " - row " + j);
}
}
我真的不确定你要做什么,但这就是我所理解的。