1

Prompt: You can test to see if an integer, x, is even or odd using the Boolean expression (x / 2) * 2 == x. Integers that are even make this expression true, and odd integers make the expression false. Use a for loop to iterate five times. In each iteration, request an integer from the user. Print each integer the user types, and whether it is even or odd. Keep up with the number of even and odd integers the user types, and print “Done” when finished, so the user won’t try to type another integer. Finally, print out the number of even and odd integers that were entered.

Here is the code I have so far:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter an integer.");
    int x = in.nextInt();
    boolean even;
    for (int i = 0; i == 5; i++) {
        if ((x / 2) * 2 == x) {
            even = true;
            System.out.println(x + " is even.");
        }
        if ((x / 2) * 2 != x) {
            even = false;
            System.out.println(x + " is odd.");
        }
    }
}

Not looking for a solution, just some help as to what I need to do. Really confused about the whole Boolean thing.

2
  • 2
    what problem are you facing ? Is it not able to print even/odd numbers as expected ? Commented Feb 24, 2014 at 1:49
  • 2
    Be careful with case of the type when you specify it. boolean is the primitive type that has values for true and false, whereas Boolean is a wrapper class that wraps the boolean type. Boolean is intended to contain true or false as well but is a class and not primitive. Commented Feb 24, 2014 at 1:54

5 Answers 5

2

This seems to be like your homework.

Seems like your 'boolean even' is not even being used, I would suggest that you don't declare nor use it. Use x = x%2 to get the number if it is even or odd is better. If it is even x should be 0, if it is odd x should be 1. % is equivalent to MOD

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
int even = 0;   // keep tracks the number of even
int odd = 0;    // keep tracks the number of odd
for (int i = 0; i < 5; i++) {
    System.out.println("Enter an integer.");
    x = in.nextInt();
    if (x % 2 == 0) {
        even++;
        System.out.println(x + " is even.");
    }
    if (x % 2  == 1) {
        odd++;
        System.out.println(x + " is odd.");
    }
}
System.out.println("Done");
System.out.println("Evens: " + even "\nOdds: " + odd);
}

This code should be the answer for your homework requirement. Your in.nextInt() should be inside the for loop since you need to request the user 5 times. Not only that, your loop should be < 5 as it will loop 5 times from 0, 1, 2, 3, 4

Sign up to request clarification or add additional context in comments.

Comments

1

Well, your loop won't fire; i == 5 is always going to be false every time you reach the loop.

What you may want to change your loop statement to be would be:

for (int i = 0; i <= 5; i++) {
    // code
}

Further, by virtue of the way Java evaluates branches, the variable even may not have been initialized. You need to instantiate it with a value.

boolean even = false;

Finally, the most straightforward way to tell if a number is even is to use the modulus operator. If it's divisible by two, it's even. Otherwise, it's odd.

if (x % 2 == 0) {
    // even, do logic
} else {
    // odd, do logic
}

You are missing a requirement from the assignment - that is, the ability to keep a running tally of the number of odd and even numbers, but I leave that as an exercise to the reader.

7 Comments

The expression (x / 2) * 2 == x will NOT always evaluate to true. The variable x is an integer. Moreover, the part that you've "left as an exercise" was the whole point of the question.
I'm really having a hard time mathematically seeing how that won't always reduce to x == x. Because, that's what I'm getting when I work it out by hand, and when I throw it at a few CAS systems (Wolfram Alpha included).
Why don't you try it out, and see for yourself why that's only true for even integers?
Integer division strikes again. Fair enough. I'll go ahead and remove that part of my answer. Further, the part I left as an exercise is, in hindsight, the least trying of the other errors I noticed. I don't want to give away all of the answer, and that's also not what they're asking for. I can admit when I'm wrong though, and I've corrected my answer accordingly. I will still leave the running tallies as an exercise, since again - not seeing that as the big problem here.
@FlorentBayle - that's not true, because even is a local variable. The page you linked to explains that local variables work differently.
|
0

The part that you're missing is keeping track of how many even and how many odd numbers have been encountered. You'll want two separate int variables for this, which you'll declare before your main loop.

int numEvens = 0;
int numOdds = 0;

Then, in the branches where you work out whether the entered number is odd or even, you'll increment one or other of these numbers.

Lastly, at the end of your program, you can print them both out in a message.

Comments

0

if you want to do this with java boolean..i think this might help you

package stackOverFlow;

public class EvenOddNumber {


public boolean findEvenOdd(int num) {
    if (num % 2 == 0) {
        return true;

    }
    else {

        return false;
    }

}
}




import java.util.Scanner;

public class Demo {
public static void main(String[] args) {
    int num;
    EvenOddNumber e = new EvenOddNumber();
    System.out.print("Enter a number:");
    Scanner scan = new Scanner(System.in);
    num = scan.nextInt();

    System.out.println( num+"  is even number?: " + e.findEvenOdd(num));

}

}

Comments

-1

A simpler way to find even and odd values is to divide number by 2 and check the remainder:

if(x % 2 == 0) // remainder is 0 when divided by 2
{
//even num
}
else
{
//odd num
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.