mardi 24 novembre 2015

Counting the occurence of integers from 1 to 10 in a sequence of 100 random numbers [on hold]

Hi I'm trying to create an array with 10 elements then generate a 100 random ints scaling from 1 to 10. The array shall contain how many times each number is generated. For example [2,5,9,5,4,6,5,8,6,4] here we got 2 ones, 5 twos, 9 threes etc. have been generated Then show the results some how.

I've tried but only come as far as generating the numbers! (I'm new to programming) So how do you count the number of times an int has been generated?

public static void main(String[] args) {
    countNumbers();
}

public static void countNumbers() {
    Random generator = new Random();
    int arr[] = new int[101];
    for (int i = 0; i < 100; i++) {
        arr[i] = generator.nextInt(10);
    }
}

Would be glad to receive some sort of help. Thanks!




Aucun commentaire:

Enregistrer un commentaire