Java 我需要在数组中插入随机生成的数字

Java 我需要在数组中插入随机生成的数字,java,Java,我已经用类似的词语或意图浏览了这些问题,但似乎找不到一个对我有帮助的 我已经使用用户输入创建了一个数组,我需要生成100到10000之间的数字并将其插入数组中。我已经创建了数组,它显示为用零填充,我已经生成了随机数,但我似乎无法将随机生成的数插入数组 这是到目前为止我的代码 import java.util.*; public class Print2DArray { public static void main(String[] args) { System.o

我已经用类似的词语或意图浏览了这些问题,但似乎找不到一个对我有帮助的

我已经使用用户输入创建了一个数组,我需要生成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();
        }
    }
}
import java.util.*;
公共类Print2DArray{
公共静态void main(字符串[]args){
System.out.println();
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“输入行数”);
int rows=userInput.nextInt();
System.out.println(“输入列数”);
int columns=userInput.nextInt();
int[][]数组=新的int[行][列];
对于(int a=0;a
注意:

Enter the number of rows: 3
Enter the number of columns: 2
574 5286 
1550 5259 
8343 4877
  • 只创建一次随机变量,而不是在double for循环的每次迭代中
  • 如果您想要100到10000(含),它需要是
    nextInt(10000-100+1)+100
  • 为您提供[09990]或[09989],因此从表达式中获得的最大值是9999,而不是10000

您没有将rand分配给数组元素。 你的第二个for循环应该是

Random r = new Random();
for(int i = 0; i<rows; i++)
{
    for(int j = 0; j<columns; j++)
    {

        array[i][j] = r.nextInt(900) + 100;
        System.out.print(array[i][j] + " ");
    }
    System.out.println();
}
Random r=new Random();

对于(int i=0;i你需要做3件事

  • 将随机值分配到数组中

  • 对于循环
,只使用一个嵌套的

  • 不要在循环内初始化随机r
  • 试试这个:

    int[][] array = new int[rows][columns];
    Random r = new Random();    
    for (int a = 0; a < rows; a++)
        for (int b = 0; b < columns; b++) {
            array[a][b] = r.nextInt(10000 - 100) + 100;
            System.out.print(array[a][b]);
        } 
        System.out.println();
    }
    
    int[][]数组=新的int[行][列];
    随机r=新随机();
    对于(int a=0;a
    array[i][j]=rand;
    在哪里插入它???类似于
    array[i][j]=rand
    ^他们说了什么。在生成随机数之前,你也在打印数组元素。谢谢你的帮助!无论何时,只要你想得到一个随机整数,创建一个新的
    random
    对象都不是一个好主意。@Gosu:你是对的,但我更关注问题,而不是代码refactoring@ThisIsntAUserName很高兴这有帮助!:)请注意答案中提到的要点。
    Random r = new Random();
    for(int i = 0; i<rows; i++)
    {
        for(int j = 0; j<columns; j++)
        {
    
            array[i][j] = r.nextInt(900) + 100;
            System.out.print(array[i][j] + " ");
        }
        System.out.println();
    }
    
    int[][] array = new int[rows][columns];
    Random r = new Random();    
    for (int a = 0; a < rows; a++)
        for (int b = 0; b < columns; b++) {
            array[a][b] = r.nextInt(10000 - 100) + 100;
            System.out.print(array[a][b]);
        } 
        System.out.println();
    }