www.webdeveloper.com
Results 1 to 3 of 3

Thread: Arrays

  1. #1
    Join Date
    Aug 2004
    Posts
    61

    Arrays

    hello, just wondering, i have an array which is storing 6 different random numbers. i need to write 4loop after words to determin how many even integers there are, how owuld i go about doin that. heres what i have so far.
    '
    thnks


    public class ArrayLab{

    public static void main(String[] args){

    int countEven = 0, countBig = 0;

    int[] numbers = new int[6];

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

    numbers[i] = (int)((25 - 1 + 1) * Math.random() + 1 );
    }
    }
    }

  2. #2
    Join Date
    Dec 2004
    Posts
    2
    After Your For() statement you should have an if statement using the mod operator.


    int count=0;
    for(int i=0;i<numbers.length;i++)
    {
    if(numbers[i]%2==0)
    {
    count++;
    }
    }

    The mod operator returns a remainder.

  3. #3
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Could you not just use
    Code:
    int count = (numbers.length / 2) + (numbers.length % 2);
    ?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles