Fill an array with random numbers

后端 未结 13 2279
感动是毒
感动是毒 2020-12-03 11:54

I need to create an array using a constructor, add a method to print the array as a sequence and a method to fill the array with random numbers of the type double.

He

13条回答
  •  被撕碎了的回忆
    2020-12-03 12:23

    You can call the randomFill() method in a loop and fill your array in the main() method like this.

    public static void main(String args[]) {
        for (int i = 0; i < anArray.length; i++) {
            anArray[i] = randomFill();
        }
    }
    

    You would then need to use rand.nextDouble() in your randomFill() method the array to be filled is a double array. The below snippet should help you get random double values to be filled into your array.

    double randomDoubleValue = rand.nextDouble();
    return randomDoubleValue;
    

提交回复
热议问题