Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Help with program

    i am writing a program for my java class due tomorrow and im stuck.
    im trying to have my program generate 100 random numbers between 0 and 9 and then count how many time each number was generated.
    for example.
    2
    6
    3
    6
    8
    4
    2
    4
    9
    5

    0-0
    1-0
    2-2
    3-1
    4-1
    5-1
    6-2
    7-0
    8-1
    9-1

    Code:
    int[] count= new int[9];
    		int x;
    		int y;
    
    		for(y=0;y<=99;++y)
    		{
    			int num=(int)(Math.random() * 10);
    			
    			for(x=0;x<=9;++x)
    			{
    				if(num==x)
    					count[x]=count[x]+1;
    			}
    		}
    		
    		for(x=0;x<=9;++x)
    			System.out.println(count[x]);

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    where are u stuck?
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    only thing i had to change to make it work was the array to hold the count:
    int[] count= new int[10];

    you need an array to hold 10 positions, not 9
    I am the luckiest man in the world

  • Users who have thanked Roelf for this post:

    porknbeans (12-18-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •