Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help!

    Hi I'm barely learning how to write java code and I need help with an assignment. The assignment consists of making an array that creates fifty random integers between zero and nine and displays the count to each number. Up to now I only know how to create the random number generator BUT I dont know how to display the count to each number(for example if the output was twenty zeros and maybe five 4's etc. from the fifty random numbers from zero to nine). This is what I have up to now:
    int i= 0;
    for{i=0; i<50; i++)
    {
    double x= Math.random();
    int random = (int)(x*9);
    System.out.println(random);
    }
    Thanks I really appreciate all help.

  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help!

    I'm not sure why you need an array of 50 integers, but here's the drill:

    Create an array of 50 ints. Suppose the name of the array is iArray.

    Then the loop is executed for i = 0, 1, ..., 49, so each time through the loop you can store the random number in iArray[i].

    Now, supposing that the random numbers that you generate have values 0, 1, ..., 9, to be able to tell the number of occurrences of each of the digits 0, 1, ..., 9, you need an array of ten ints. Suppose the name of the array is counts.

    As you create the random digits 0, 1, ... , 9 in the loop you want to do the following:

    If the random digit is 0, increment counts[0].

    If the random digit is 1, increment counts[1].

    .
    .
    .

    If the random digit is 9, increment counts[9].

    Now you don't need ten if statements to tally the counts; If you think about it, you should see that you can simply do the following each time through the loop:
        ++counts[random]; // Assumes that random has value 0, 1, ... , 9

    Then, after all of the numbers have been generated, make a loop that shows the values of all of the counts:
            for (int i = 0; i < 10; i++) {
                System.out.println("counts[" + i + "] = " + counts[i]);
            }

    Or some such thing.


    Cheers!

    Z

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    erer2116 (November 12th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help!

    Thank you! This really helped