mardi 29 mars 2016

Java: Give random double numbers in array

My task is to make a program like this:

give size of array:
-5
size < 0.
Give size of array: 5
1.: 1.77379378756135961,77
2.: 4.7920617665887924,79
3.: 24.2215470529474524,22
4.: 27.29623562686216327,3
5.: 9.8175915340430049,82

Here's my code: (and below my run)

    Scanner keyb = new Scanner (System.in);
    int size;

    do {
        System.out.println("give size of array");
        size= keyb.nextInt();
        keyb.nextLine();
        if (size<= 0) {
            System.out.println("size < 0");
        }
    } while (size<= 0);    

    double[] array= new double[size];
    Random generator = new Random(); 

    for (int i = 0; i < array.length; i++) {
        array[i] = generator.nextDouble();
        System.out.println((i + 1) + ".: " + array[i]);
    }

But my problem is I keep getting numbers between 0 and 1, what am I doing wrong?

1.: 0.4189300484903713
2.: 0.7600502254378542
3.: 0.3307584239116951
4.: 0.2617296585600827
5.: 0.8422085114389094




Aucun commentaire:

Enregistrer un commentaire