2

I'm pretty sure this should work... But even if I enter a 0 or a 1 it still asks me to pick a colour. Am I being stupid or should it be exiting the loop if I enter a 0 or a 1?

public static int setColour() {
    EasyReader keyboard = new EasyReader();
    int colour;
    do{
        colour = keyboard.readInt("Pick a colour (black = 0, white = 1): ");
    }while (colour != 0 || colour != 1);
    return colour;
}
2
  • 3
    You need and (&&), not or (||) Commented May 13, 2015 at 23:30
  • read online about conditional operators. Commented May 13, 2015 at 23:43

2 Answers 2

7

Both conditions will never be satisfied simultaneously using the || operator. You want

} while (colour != 0 && colour != 1);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, for some reason I thought using && meant 0 AND 1 to make it exit the loop and || meant either 0 OR 1 to exit the loop.. But thanks! :)
0

Using || will never return the correct result. The && operator however will return your answer accurate.

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.