Results 1 to 9 of 9
Thread: Even or odd?
- 04-09-2011, 05:42 PM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
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:
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.Java 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"); } } }
I would appreciate some advice
- 04-09-2011, 06:01 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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.
Java 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:
when declaring them and the problem is solved.Java Code:int evens = 0; int odds = 0;
But if I understand correctly:
you have to count how many evens and odds there are and not to sum them. Use:whether there were more odd numbers or even numbers
Java Code:evens++; and odss++;
instead of
Java Code:evens = num1 + num2 + num3 + num4 + num5; odds = num1 + num2 + num3 + num4 + num5;
- 04-09-2011, 06:05 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
And of course you must work on your logic because this won't work properly
- 04-09-2011, 07:22 PM #4
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Wow, I feel like an idiot. I don't know why I didn't see that.
The same is if else is executed, then "variable evens might not be initialized"
Just put:
when declaring them and the problem is solved.Java Code:int evens = 0; int odds = 0;
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.
But if I understand correctly:
you have to count how many evens and odds there are and not to sum them. Use:
Java Code:evens++; and odss++;
instead of
Java Code:evens = num1 + num2 + num3 + num4 + num5; odds = num1 + num2 + num3 + num4 + num5;
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:
So the code works fine now. Is there a way to combine all of these individual if statements?Java 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"); } } }
- 04-09-2011, 08:40 PM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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 #6
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
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 #7
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
- 04-09-2011, 10:52 PM #8
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
- 04-09-2011, 10:52 PM #9
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks