Results 1 to 9 of 9

Thread: Even or odd?

  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default 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:

    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");
        	}
        }
    }
    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.

    I would appreciate some advice

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    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:

    Java Code:
    int evens = 0;
    int odds = 0;
    when declaring them and the problem is solved.

    But if I understand correctly:
    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:

    Java Code:
    evens++;
    
    and
    
    odss++;

    instead of

    Java Code:
    evens = num1 + num2 + num3 + num4 + num5;
    
    odds = num1 + num2 + num3 + num4 + num5;

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    And of course you must work on your logic because this won't work properly

  4. #4
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default


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

    Java 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.


    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;
    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:

    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");
        	}
        }
    }
    So the code works fine now. Is there a way to combine all of these individual if statements?

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    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.

  6. #6
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    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.

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    Quote Originally Posted by imorio View Post
    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...

  8. #8
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by milovan View Post
    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.

  9. #9
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Smile

    Quote Originally Posted by imorio View Post
    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.

Posting Permissions

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