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 4 of 4

Thread: help with an accumulator

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with an accumulator

    i have an assignment where i use a random generator to get a number 1 through 6. i wrote the code to do this 10,000 time and i need to count how many times each number comes up. please help me, here is what i have so far.
    import java.util.Scanner;
    import java.util.Random;

    public class NumberGenerator2
    {
    public static void main(String[] args)
    {
    int number1;
    int count;
    int max;

    max = 10000;

    for ( count = 1; count <= max; count++)
    {
    Random randomNumbers = new Random();

    number1 = randomNumbers.nextInt(6);

    System.out.println(number1 +1);

    }
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: help with an accumulator

    Use an int array. For each random number increment one of the ints in the array. This can be done with a single line of code for all numbers 1 - 6.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with an accumulator

    i havent learned about arrays yet i have been looking though the chapter on arrays and i cant figure out how to put the out put into an array could you please explain in more detail because i just dont know what im doing

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: help with an accumulator

    If you haven't learnt arrays then just use 6 variables. Use an if statement to determine which variable to increment.
    Improving the world one idiot at a time!

  5. The Following User Says Thank You to Junky For This Useful Post:

    dann (October 12th, 2013)

Similar Threads

  1. help on simple accumulator
    By Khalon in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 11:39 PM