vendredi 24 août 2018

Array with random integers

I need and array that generates 10 random numbers between 1 and 5, inclusive. I then need to display the number of times 1-5 shows up in the array. This is what I have so far, it runs fine, it's just that the number of occurrences for the numberes 1-5 totals different everytime, which it should total exactly 10 everytime since the array can only hold 10 integers.

public static void main(String[] args){
    //create an array that will produce 10 numbers
    int[] randArray = new int[10];

    //initialize randArray to the values 1-5
    for(int i = 0; i < 6; i++){
        randArray[i] = (int)(Math.random() * (6 - 1)) + 1;
    }

    System.out.println("Number\t" + "Appears");

    for(int x = 1; x < 6; x++){
        System.out.println(x + "\t" + randArray[x]);
    }
}




Aucun commentaire:

Enregistrer un commentaire