我已经查看了类似词语或意图的问题,但似乎找不到能帮助我的问题。我需要将随机生成的数字插入到数组中
我已经使用用户输入创建了一个数组,我需要生成100到10000之间的数字并将其插入到数组中。我已经创建了数组,它显示为填充零,并且生成了随机数,但我似乎无法将随机生成的数字插入到数组中。
这是我的代码到目前为止。
import java.util.*;
public class Print2DArray {
public static void main(String[] args) {
System.out.println();
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = userInput.nextInt();
System.out.println("Enter the number of columns");
int columns = userInput.nextInt();
int[][] array = new int[rows][columns];
for (int a = 0; a < rows; a++)
for (int b = 0; b < columns; b++)
array[a][b] = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array[i][j]);
{
Random r = new Random();
int rand = r.nextInt(10000 - 100) + 100;
}
}
System.out.println();
}
}
}
'阵列[i] [j] = RAND;' –
你在哪里插入它???像'array [i] [j] = rand'这样的东西^ – Codebender
^他们说什么。您还在打印数组元素*之前*生成随机数 – samgak