dimanche 2 octobre 2016

I am having trouble with a programming exercise. It involves random numbers and arrays

So I need help with this problem. I am trying to generate 100 numbers between 0 and 9 and store them in an array, I'm also trying to then store the count for each of the numbers and then display it like "Number 1 appeared __ times!" at the end of the program. I don't know what to do but here is my code:

//Exercise 7.7 --> program to generate 100 random integers from 1-10 and display count for each number

    //create array to store integers from 1 - 10
    int[] randoms = new int[101];
    int[] counts = new int[10];
    Random gen = new Random();


    //for loop to fill the array with random number from 1-10
    int k;
    for(int i = 1; i < 101; i++){
        k=i;
        randoms[i] = gen.nextInt(10);
        for (int r : randoms) counts[r]++;
    }

    //for loop to print out the number of numbers

    for (int o = 0; o < counts.length; o++) {
        System.out.println(o + " occurs " + counts + " times!");
    }




Aucun commentaire:

Enregistrer un commentaire