Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Odd and Even numbers with boolean?

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Odd and Even numbers with boolean?

    Okay, so I'm trying to figure out how to determine if an integer is even or odd by using a boolean method. I think I have the method right, but it's calling the method into the main that has got me stumped. I've been out of practice for a couple weeks, so I'm very confused.
    import java.util.Scanner;
     
    public class Odd_Even {
    	public static void main(String[] args) {
    		//Scanner, variables
    		Scanner input = new Scanner(System.in);
    		int number;
     
    		System.out.println("Please enter an integer number: ");
    		number = input.nextInt();
     
    		//output
    		System.out.println("The number " + number + " is " + )
    		if(number = iseven(number)) {
     
    		}
    	}
    	private static boolean iseven(int number) {
    		if(number % 2 == 0) {
    		}
    		return true;
     
    	}
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Odd and Even numbers with boolean?

    you must have two return statement in iseven method.
    check if it is even number using modulo and if it is even return true otherwise return false.

    you have only 1 return statement in your method iseven (return true). in that case, it will always returns true even the parameter is odd number.

  3. The Following User Says Thank You to dicdic For This Useful Post:

    Elyril (April 14th, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Odd and Even numbers with boolean?

    Alright, so I changed it to:
    private static boolean iseven(int number) {
    		if(number % 2 == 0) {
    			iseven = true;
    		}
    		else {
    			iseven = false;
    		}
     
    	}
    But now it says iseven can't be resolved to a variable, so do I have to make a different variable like
    private static boolean iseven(boolean iseven, int number) {
    		if(number % 2 == 0) {

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Odd and Even numbers with boolean?

    But now it says iseven can't be resolved to a variable, so do I have to make a different variable like
    because iseven is not defined.
    it is either you declare a boolean data whose variable name is iseven before the of-else statement. after the if-else statement return it return iseven;

    or

    return a boolean expression inside if and else

  6. The Following User Says Thank You to dicdic For This Useful Post:

    Elyril (April 14th, 2014)

  7. #5
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Odd and Even numbers with boolean?

    Nevermind.. I got it. Sorry, I was just being stupid and lazy for that last one.

Similar Threads

  1. Adding odd numbers from 1 to 15
    By kcassar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 9th, 2013, 07:24 AM
  2. Odd and Even Numbers
    By tyb97 in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 30th, 2012, 04:19 PM
  3. sum of arrays and printing even and odd numbers in the array
    By senecawolf in forum Collections and Generics
    Replies: 3
    Last Post: November 8th, 2011, 03:07 PM
  4. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  5. Odd and Even Numbers Logics in FOR and IF Loop methods
    By mparthiban in forum Loops & Control Statements
    Replies: 2
    Last Post: May 12th, 2010, 05:19 AM