mercredi 3 janvier 2018

Using shorter version of or "||" in an If statement with integers and Math/Logic error?

So I finally got my code to work with:

if((choice == 1 ) || (choice == 2) || (choice == 3) || (choice == 4)){

but why does:

if(choice == (1 | 2)) {

result in a logic/math error? for example, if I enter "3" then the code accepts it and processes it as successful. The code snippet is as follows:

while(counter == 0){
        try{
            int choice = Integer.parseInt(input.nextLine());

            if(choice == (1 | 2)){
                System.out.println("You got it!");
                ++counter;
            }else{
                System.out.println("Try again");
            }
        }
        catch(NumberFormatException Exception){
            System.out.println("You did something wrong");
        }


    }

And if I do:

if(choice == (1 | 2 | 3 | 4 )){

then it will only accept 7 as far as I can tell. What exactly is going on when it is compiling and is there a way to shorten the solution I found?

Aucun commentaire:

Enregistrer un commentaire