Design a java program that generates 100 random numbers, and keeps a count of how many of those random numbers are even and how many are odd.
This is what I have and it is not suppose to be an array just simple coding with a boolean.

import java.util.Random;

   public class RandomNumbers 
   {
      public static void main(String[] args) 
      {
      // Create a Random object
       Random randomNumbers = new Random();

      //Declare Variables
         int count;
         int number;
         int odd;
         int even;
      //Loop displays 100 random numbers 
       for (count = 0; count <= 100; count++) 
       {
         number = randomNumbers.nextInt()+1;
         System.out.println(number);

        if((number % 2) == 0) // remainder function
            int even;
            else
             int odd;
            return even;
            }
          System.out.println("This is the amount of even:"+even);
          System.out.println("This is the amount of odd:"+odd);



         }         
       }
      } 

Recommended Answers

All 6 Replies

Line 16 looks like you'll get 101 numbers.
Why not initialize even and odd to zero on lines 13 and 14.
Line 22 would be simply even++ and I'll let you guess what line 24 would be.

What is this about boolean?

You also have a problem on line 25. As soon as this is executed it will return from the current method - which is the main method, so your program will then terminate without processing any more numbers.
Your code will be almost un-debuggable if you persist on incorrect indentation. Omitting the brackets on 1-line if/else blocks also can cause great confusion. For example

        if((number % 2) == 0) // remainder function
            int even;
            else
             int odd;
            return even;  // looks like part of the else block
            } // looks like part of the if construct

... whereas what you have really coded, and how the compiler will see it, is

        if((number % 2) == 0) { // remainder function
            int even;
        } else {
            int odd;
        }
        return even;
  }

To make things worse you have 3 open curly brackets but 4 closes, so that code can't compile anyway.

        if((number % 2) == 0) { // remainder function
            int even;
        } else {
            int odd;
        }

makes no sense anyway...

You probably want

        (number%2==0)?even++:odd++;
commented: Indeed +15

If we keep optimizing we see we don't need to track both odd and even. We could save many CPU cycles by only counting odd or even then for the other value, use something like 100 - even for the odd count. One less variable, many fewer CPU cycles and less CO2 emissions.

commented: Planet saved! +15

You don't even need condition or mod operator. odd += number & 1; even less CO2 emissions. The world is greener.

Incorporating many of the suggestions the others have stated and mixing it up with my own, the code can be like this.

import java.util.Random;

public class RandomNumbers 
{
    public static void main(String[] args) 
    {
        Random randomNumbers = new Random();
        int count, number, odd;
        for (count = 0; count < 100; count++){
            number = randomNumbers.nextInt()+1;
            System.out.println(number);

            if((number % 2) != 0) {
                odd++;
            }
        }

        System.out.println("This is the amount of even:" + (100 - odd));
        System.out.println("This is the amount of odd:" + odd);
    }         
}

I also removed the comments that are unnecessary.

commented: Just one thing. Initialize odd to zero. Don't count on it being zero. +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.