## 13 Replies - 25381 Views - Last Post: 02 August 2013 - 08:48 AM

### #1

# Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 11:52 AM

Hopefully I am doing this post right, I am having problems counting the even's and odd's in this code. I have tried a method (which I have taken out of this code) but I can't get that to work. Thanks!

import java.util.Random;

public class EvenOdd {

public static void main(String[] args) {

Random number = new Random();

System.out.println("Testing the number of Odd and Even in a list of 100" + "\n" + "randomly generated values in the range of 1-1000.");

for (int i = 0; i < 100; i++) {

System.out.println();

}

int countEven = 0;

int countOdd = 0;

if ((number % 2) == 0){

countEven++;

}

else {

countOdd++;

}

System.out.println("Even's: " + countEven);

System.out.println("Odd's: " + countOdd);

}

}

import java.util.Random;

public class EvenOdd {

public static void main(String[] args) {

Random number = new Random();

System.out.println("Testing the number of Odd and Even in a list of 100" + "\n" + "randomly generated values in the range of 1-1000.");

for (int i = 0; i < 100; i++) {

System.out.println();

}

int countEven = 0;

int countOdd = 0;

if ((number % 2) == 0){

countEven++;

}

else {

countOdd++;

}

System.out.println("Even's: " + countEven);

System.out.println("Odd's: " + countOdd);

}

}

##
**Replies To:** Counting Even and Odd numbers from a random list of numbers

### #2

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 12:06 PM

Hey, welcome to the forums To use the code tags you need the beginning code tag (the one without the /) in front of the code and the other behind the code.

Now, what you are creating is a random object. This object can be used to generate random numbers so trying to divide this object would not work. Instead you should create an array and cycle through it assigning random integers to each slot in the array. You could even have a random array length if you wanted. It would look something like this:

Hope this helps

Btw, if you want the random object to only generate numbers within a certain range use random.nextInt(someInt). The generated number would be between 0 and the number, including 0 and excluding someInt.

Now, what you are creating is a random object. This object can be used to generate random numbers so trying to divide this object would not work. Instead you should create an array and cycle through it assigning random integers to each slot in the array. You could even have a random array length if you wanted. It would look something like this:

int [] integers = new int [number.nextInt()]; for(int i = 0; i < integers.length; i++) { integers[i] = numbers.nextInt(); //to make the code more concise you could even do the counting in this loop if(integers[i] % 2 == 0) countEven++; else countOdd++; } //your even and odd count printing //if you wanted you could make another loop to print out the string of numbers by looping through the array and printing each number followed by a comma, for example

Hope this helps

Btw, if you want the random object to only generate numbers within a certain range use random.nextInt(someInt). The generated number would be between 0 and the number, including 0 and excluding someInt.

This post has been edited by **StrongJoshua**: 01 August 2013 - 12:08 PM

### #3

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 12:17 PM

Yes that makes a lot of sense, thank you very much! I just looked my assignment again and I need to do a method named isEven that accepts an int argument. Here is what I have for that, which, of course doesn't work.

//I don't know what to use for the parameters public static int isEven (int ????){ if((number % 2) == 0) { } return even++; else if((number % 2) != 0) } return odd++; System.out.println("Even's: " + even); System.out.println("Odd's: " + odd);

### #4

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 03:20 PM

Are you sure you're supposed to use a method that returns an integer instead of a boolean? Usually something that sounds like isEven or isOdd sounds like it would be a boolean.

i.e.

Then you'd cycle through the array and use,

i.e.

boolean isEven(int num) { if((num % 2) == 0) return true; return false; }

Then you'd cycle through the array and use,

for(int i = 0; i < array.length; i++) { if(isEven(array[i])) evenCount++; else oddCount++; }

### #5

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 09:19 PM

I know this forum isn't for doing homework but here is my assignment as arrays and the boolean wasn't specified. Thanks for the reply, I can't even get a response from her to help most of the time.

Part 1: Odd or Even

Write a program with a method named isEven that accepts an int argument. The method should return true if the argument is even, or false if it’s odd. The program’s main method should use a loop to generate 100 random integers in the range of 1 - 1000. It should use the isEven method to determine whether each random number is even, or odd. When the loop is finished, the program should display the number of even numbers that were generated, and the number of odd numbers.

Testing the number of Odd and Even in a list of 100 randomly generated values in the range of 1 – 1000.

Even’s: 57

Odds: 43

You can use the following logic to determine whether a number is even or odd:

if ((number % 2) == 0)

{

// The number is even.

}

else

{

// The number is odd.

}

Once you have your program working, I want you to run it 10 times in a loop and display the output for each loop along with the totals for all 10 iterations.

Your output should closely resemble: (i.e. this is the level of detail I want to see. Yours does not have to look exactly like this as long as it should the results of each iteration AND the totals.

Part 1: Odd or Even

Write a program with a method named isEven that accepts an int argument. The method should return true if the argument is even, or false if it’s odd. The program’s main method should use a loop to generate 100 random integers in the range of 1 - 1000. It should use the isEven method to determine whether each random number is even, or odd. When the loop is finished, the program should display the number of even numbers that were generated, and the number of odd numbers.

Testing the number of Odd and Even in a list of 100 randomly generated values in the range of 1 – 1000.

Even’s: 57

Odds: 43

You can use the following logic to determine whether a number is even or odd:

if ((number % 2) == 0)

{

// The number is even.

}

else

{

// The number is odd.

}

Once you have your program working, I want you to run it 10 times in a loop and display the output for each loop along with the totals for all 10 iterations.

Your output should closely resemble: (i.e. this is the level of detail I want to see. Yours does not have to look exactly like this as long as it should the results of each iteration AND the totals.

### #6

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 09:32 PM

### #7

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 05:59 AM

Schutzzz, let me rewrite your method slightly differently:

The if basically says "If true then return true; it false return false."

Why not just return the value of answer, or even this:

boolean isEven(int num) { boolean answer = ((num % 2) == 0); if(answer) return true; return false; }

The if basically says "If true then return true; it false return false."

Why not just return the value of answer, or even this:

boolean isEven(int num) { return ((num % 2) == 0); }

### #8

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 06:52 AM

### #9

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 07:27 AM

Thanks everyone for the replies! I will work on it today.

Nick

Nick

### #10

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:28 AM

You don't even need an else

int count = 100; int even, odd; for (int i = 0; i < count; i++) { if (i % 2 == 0) even++; } odd = count - even;

### #11

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:35 AM

You don't even need an if:

for(int i : array) { odd += (i % 2); } even = array.length - odd;

### #12

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:39 AM

### #13

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:46 AM

Yes, a method, isEven is even is a much better way to do it.

This post has been edited by **cfoley**: 02 August 2013 - 08:46 AM

### #14

## Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:48 AM

nvm

This post has been edited by **CasiOo**: 02 August 2013 - 08:48 AM

Page 1 of 1