lundi 18 juin 2018

Is it possible to get the sum of these random generated doubles in an array?

Is there a way to get the average of these randomly populated doubles in this array? I want to make an array that is filled with randomly generated doubles based on the user inputs length.

import java.util.Scanner; 
    import java.util.Random;
    import java.util.Arrays;
        public class GarageTest {

        public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a number");
        //create an array with the size entered by the user
        double[] array = new double[scanner.nextInt()];
        //populate the array with doubles
        for(int i =0; i < array.length; i++) {
            Random rand = new Random();
            array[i] = rand.nextDouble();
        }
        System.out.println(Arrays.toString(array));
        }

        }




Aucun commentaire:

Enregistrer un commentaire