Even or odd?

• 04-09-2011, 05:42 PM
jim01
Even or odd?
I have to write a program to input 5 integers from the keyboard and print whether there were more odd numbers or even numbers. I have created a program I thought would work but it doesn't compile. This is the program:

Code:

``` import java.util.Scanner; public class OddOrEven {     public static void main(String[] args) {             //declare variable             int num1, num2, num3, num4, num5, evens, odds;             //Create new Scanner             Scanner input = new Scanner(System.in);             //Ask user to input five integers             System.out.println("Please input five integers");             num1 = input.nextInt();             num2 = input.nextInt();             num3 = input.nextInt();             num4 = input.nextInt();             num5 = input.nextInt();             //Find if odd or even             if(num1 % 2 == 0 || num2 % 2 == 0 || num3 % 2 == 0 || num4 % 2 == 0 || num5 % 2 == 0) {                           evens = num1 + num2 + num3 + num4 + num5;                     }             else {                     odds = num1 + num2 + num3 + num4 + num5;             }             //Find which is greater, even or odd             if(evens > odds){                     System.out.println("There are more even numbers than there are odd numbers");             }             else {                     System.out.println("There are more odd numbers than there are even numbers");             }     } }```
The error code I receive is "variable evens might not be initialized" and "variable odds might not be initialized." I believe these errors might be occurring because the even and odd statements found within the first if statement are localized to that statement only, but I am not sure. I tried putting the even and odd statements before the if statement and just put the variables even and odd inside of the if statement, but of course that didn't work either because it's not a statement. I can't figure out how to incorporate everything into one if statement.

• 04-09-2011, 06:01 PM
milovan
If if() condition is true and this part of code is executed then "variable odds might not be initialized." because else part is skipped and odds is never reached.

Code:

```if(num1 % 2 == 0 || num2 % 2 == 0 || num3 % 2 == 0 || num4 % 2 == 0 || num5 % 2 == 0) {   evens = num1 + num2 + num3 + num4 + num5; }```

The same is if else is executed, then "variable evens might not be initialized"
Just put:

Code:

```int evens = 0; int odds = 0;```
when declaring them and the problem is solved.

But if I understand correctly:
Quote:

whether there were more odd numbers or even numbers
you have to count how many evens and odds there are and not to sum them. Use:

Code:

```evens++; and odss++;```

Code:

```evens = num1 + num2 + num3 + num4 + num5; odds = num1 + num2 + num3 + num4 + num5;```
• 04-09-2011, 06:05 PM
milovan
And of course you must work on your logic because this won't work properly
• 04-09-2011, 07:22 PM
jim01
Quote:

The same is if else is executed, then "variable evens might not be initialized"
Just put:

Code:

```int evens = 0; int odds = 0;```
when declaring them and the problem is solved.

Wow, I feel like an idiot. I don't know why I didn't see that.

Quote:

But if I understand correctly:

you have to count how many evens and odds there are and not to sum them. Use:

Code:

```evens++; and odss++;```

Code:

```evens = num1 + num2 + num3 + num4 + num5; odds = num1 + num2 + num3 + num4 + num5;```

Yes, I see where that is the correct way now. I was making an incorrect assumption with the addition which, in hindsight, didn't make a bit of sense.

As to my logic, once I followed your above advice I could see that something was incorrect because the program would work until I entered three odd numbers and two even numbers, then it would tell me that there were more even numbers than there were odd numbers. After taking another look at the logical operator OR I realized that I could not use it the way that I was trying to do because If num1 and num2 are both odd then the output is going to be even and increment the even variable instead of incrementing the odd variable twice. At least that is how I think it is working.

So I rewrote the code and used individual if statements:

Code:

``` import java.util.Scanner; public class OddOrEven {     public static void main(String[] args) {             //declare variable             int num1, num2, num3, num4, num5, evens = 0, odds = 0;             //Create new Scanner             Scanner input = new Scanner(System.in);             //Ask user to input five integers             System.out.println("Please input five integers");             num1 = input.nextInt();             num2 = input.nextInt();             num3 = input.nextInt();             num4 = input.nextInt();             num5 = input.nextInt();             //Find if odd or even             if(num1 % 2 == 0){                           evens++;             }             else {                     odds++;             }             if(num2 % 2 == 0){                     evens++;             }             else {                     odds++;             }             if(num3 % 2 == 0){                     evens++;             }             else {                     odds++;             }             if(num4 % 2 == 0){                     evens++;             }             else {                     odds++;             }             if(num5 % 2 == 0){                     evens++;             }             else {                     odds++;             }             //Find which is greater, even or odd             if(evens > odds){                     System.out.println("There are more even numbers than there are odd numbers");             }             else {                     System.out.println("There are more odd numbers than there are even numbers");             }     } }```
So the code works fine now. Is there a way to combine all of these individual if statements?
• 04-09-2011, 08:40 PM
milovan
Well I don't know what your experience is with arrays.
You can populate an array with ints from user input and then in one for loop you can check if ints from the array meets your condition.
• 04-09-2011, 09:16 PM
imorio
Or, without using arrays, just using loops: you could start a loop, ask at the begining of the loop for the first input, check if it is even or odd and then do the loop again asking for the next number.
• 04-09-2011, 09:26 PM
milovan
Quote:

Originally Posted by imorio
Or, without using arrays, just using loops: you could start a loop, ask at the begining of the loop for the first input, check if it is even or odd and then do the loop again asking for the next number.

Even better solutions and without arrays as imorio said...
• 04-09-2011, 10:52 PM
jim01
Quote:

Originally Posted by milovan
Well I don't know what your experience is with arrays.
You can populate an array with ints from user input and then in one for loop you can check if ints from the array meets your condition.

We won't cover arrays for two more weeks. Thank you very much for you help.
• 04-09-2011, 10:52 PM
jim01
Quote:

Originally Posted by imorio
Or, without using arrays, just using loops: you could start a loop, ask at the begining of the loop for the first input, check if it is even or odd and then do the loop again asking for the next number.

We begin loops on Monday, so I guess I'll learn how to do it in a short while.