ChatNoir
ChatNoir

Reputation: 545

do while loop doesn't get out of the loop?

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

Upvotes: 2

Views: 64

Answers (2)

Javon Wilson
Javon Wilson

Reputation: 1

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

Upvotes: 0

Reimeus
Reimeus

Reputation: 159754

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

} while (colour != 0 && colour != 1);

Upvotes: 7

Related Questions