samedi 31 janvier 2015

Java - If statement if both and each

So I have two variables:



int x = 3;
int y = 7;


Then I have this code:



int key = 4; // Can be anything

if (key == x || key == y) {
if (key == x) {
// Do something with x
} else {
// Do something with y
}

// Do something with both x and y
}


I get really annoyed to have to write key == x 2 times, because in my original code that can be something really long like: key == StaticRandomVeryLongNamedClassName.getARandomNumber().


Is there any way to do this without having to write key == x two times?


Thanks.


Aucun commentaire:

Enregistrer un commentaire