为java中的行和列生成特定数字

问题描述 投票:-3回答:1

我想编写一个在循环中生成特定数字的代码。

例如生成这样的数字:

 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行

java algorithm math
1个回答
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);
    }
}

我真的不确定你想要做什么,但这是我所理解的。

© www.soinside.com 2019 - 2024. All rights reserved.