[英]Trying to create a 2d array based on given rows from user input (java)
這就是我到目前為止所擁有的。
public static int[][] generateRandomPositions(int number, int width, int height){
for(int j=0; j <number; j++){
int[][] pos = new int[][]{
{Utility.randomInt(width),Utility.randomInt(height)}
};
}
return [][]pos;
}
基本上,該方法獲得一個數字,該數字是應有的行數,而width和height是兩列中的兩個數字。 其中的那些是在給定的數字之間隨機生成的(例如Utility.randomInt(5)在0到5之間)。 我遇到的問題是弄清楚如何根據輸入的數字來創建行數。 我所不相信的東西行得通。 這是輸入這些數字后結果如何的示例。
generateRandomPositions(4, 5, 30)
int[][] posB = new int[][] {
{ 3,21 },
{ 4,15 },
{ 1,17 }
{ 3,9 }
};
因為輸入了4作為數字,所以有4行。 其他數字是隨機生成的。 所以我只需要幫助弄清楚如何根據輸入的numbers變量創建行數。 我是編程的新手,因此,建議和幫助將不勝感激。
public static int[][] generateRandomPositions(int number, int width, int height){
int[][] pos = new int[number][2];
for(int j=0; j <number; j++){
pos[j][0] = Utility.randomInt(width);
pos[j][1] = Utility.randomInt(height);
}
return pos;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.