Java 如何将数组中的随机数赋值?

Java 如何将数组中的随机数赋值?,java,arrays,loops,random,Java,Arrays,Loops,Random,我对数组和将它们赋给float之类的值有问题。这是我的密码 System.out.print("Please enter the number of iteration: "); Scanner scn = new Scanner(System.in); int inumber = scn.nextInt(); // i get the iteration number Random rnd = new Random(); float x = -

我对数组和将它们赋给float之类的值有问题。这是我的密码

System.out.print("Please enter the number of iteration: ");
    Scanner scn = new Scanner(System.in);
    int inumber = scn.nextInt(); // i get the iteration number

        Random rnd = new Random();
        float x = -10 + rnd.nextFloat() * 20; // i created random number between 10 and -10
        System.out.println("My x = " +x);

            float[] xarray = new float[4]; // created my test array
            Random arrayrnd = new Random();


        for (int i=0;i<inumber;i++) { // created a for loop with that number

            for (int j = 0; j<xarray.length;j++) {

                xarray[j] = arrayrnd.nextFloat(); // also created random for array and assigned them
            }

            Arrays.sort(xarray); // i sorted the array
            xarray[0] = x; // i tried to assign the smallest number to x but didn't work

            System.out.println("t="+i+" My new x = " +x);
System.out.print(“请输入迭代次数:”);
扫描仪scn=新扫描仪(System.in);
int inumber=scn.nextInt();//我得到了迭代次数
随机rnd=新随机();
浮点x=-10+rnd.nextFloat()*20;//我创建了10到-10之间的随机数
System.out.println(“My x=“+x”);
float[]xarray=新浮点[4];//创建了我的测试数组
Random arrayrnd=新的Random();

对于(int i=0;i如果要分配给x,应执行以下操作:

x = xarray[0];
而不是:

xarray[0] = x;

你应该在循环中移动
float x=-10+rnd.nextFloat()*20;
如果你想改变它,你一直在使用相同的x,而不是新的值
xarray[0]=x;//我试图给x分配最小的数字,但没有起作用
——你不是给x分配最小的,而是visa