1

I have the user enter a list of 5 ints into an int[]. I then go through those ints in the int[] with a simple for statement. I have an int variable declared called "evens." If the number in the int[] at i %2 == 0 , evens++;

Now I have the if statement:

if (evens !=2 || evens!=3) {
  System.out.print("This was called because " + evens + " is not equal to 2 or 3");
}

The problem is that this is being called no matter what the value in evens is. It can be 5 or 3 and still gets called. I've been using C# recently but this is simple Java.

Whole code:

int evens = 0;
    for(int i=0; i<chosenNumbers.length; i++) {
        if(chosenNumbers[i] %2 ==0)
            evens++;
    }

System.out.println("You chose "+evens+" even numbers and " + (chosenNumbers.length-evens) + " odd numbers.");

    if (evens !=2 || evens!=3) {
        System.out.print("This was called because " + evens + " is not equal to 2 or 3");
    } else if (evens==2 || evens==3) {
        System.out.print(evens +" equals 2 or 3");
    }
2
  • Try saying evens !=2 || evens!=3 in English: "if evens differs from 2 or evens differs from 3, then ....". And then you'll realize that any number always differs from 2 or differs from 5. You probably mean and instead of or. Commented Jan 12, 2016 at 1:39
  • Thanks for your response. Commented Jan 12, 2016 at 1:50

2 Answers 2

2

You need to replace:

if(evens !=2 || evens!=3) {
    System.out.print("This was called because " + evens + " is not equal to 2 or 3");
}

with:

if(evens !=2 && evens!=3) {
    System.out.print("This was called because " + evens + " is not equal to 2 or 3");
}

If you use the logical OR ||, you will always enter the block, as the only way it would not enter is it evens is equal to 2 and 3 at the same time, which isn't possible.

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

Comments

0

you can use

if(!(evens ==2 || evens==3)) {
        System.out.print("This was called because " + evens + " is not equal to 2 or 3");
} else {
        System.out.print(evens +" equals 2 or 3");
}

4 Comments

@MrAwesome8, not true ... boolean algebra can be correctly applied: !(a || b) is equivalent to !a && !b. This is part of De Morgan's laws. Devils and Details ...
@JoD. Exactly, and that's why this answer is "Not Useful" as it gives the same answer as AntonH gave 12 minutes earlier, but without any explanation. There's no need for duplicate answers.
@ErwinBolwidt I can agree to that as a reason for flagging/downvoting, but the first comment should be retracted as incorrect.
Also @Heshan was earlier to respond and did incorporate the simpler else in his answer. That is no need for the else if (...). Would be nice if the poster would highlight that in his answer. If not the accepted answer can highlight that part too.

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.