lundi 22 avril 2019

How to fill array with random numbers limited to 0-20

I am having trouble placing the random generator in the array, how can I get #20 random numbers from 0-9 in the array? Then you count the occurrences of those numbers.

import java.util.Random;

public class CountDigits {

public static void main(String[] args) {



    Random digit = new Random(); 

    int Random[] = new int [20];

    int Digits[] = {0,1,2,3,4,5,6,7,8,9}; 

    int Count [] = new int [10]; 


    for ( int i = 0; i < Digits.length; i++) {


        for( int j = 0; j < Random.length; j++) {

            if ( Digits [i] == Random [j] ) 
                Count[i]++; 
        }

    }


    for ( int i = 0; i < Count.length; i++) {

        if ( Count[i] == 1) 
            System.out.printf(" %d  occurs  1 time " , Digits[i] ); 

        else
            System.out.printf("%d occurs %d times" , Digits[i], Count[i]); 

    }

result so far:: 0 occurs 20 times1 occurs 0 times2 occurs 0 times3 occurs 0 times4 occurs 0 times5 occurs 0 times6 occurs 0 times7 occurs 0 times8 occurs 0 times9 occurs 0 times




Aucun commentaire:

Enregistrer un commentaire