samedi 9 mai 2015

Why can't I replace if statement with conditional operator (?:)?

I've been struggling to turn

private PlaneClass preferredClass;

if (preferredClass == PlaneClass.FIRST_CLASS)
    preferredClass = PlaneClass.ECONOMY_CLASS;
else
    preferredClass = PlaneClass.FIRST_CLASS;

into

preferredClass == PlaneClass.FIRST_CLASS ? 
                preferredClass = PlaneClass.ECONOMY_CLASS 
                               : preferredClass = PlaneClass.FIRST_CLASS;

The if-statement compiles. The conditional operator doesn't. (Error messages: 1. Type mismatch: cannot convert from PlaneClass to boolean 2. syntax error on token "=". And two other error...). Where did I go wrong?

Aucun commentaire:

Enregistrer un commentaire