How to turn if, else if logic into a ternary operator?
问题 I was just wondering if this was possible because i started using ternary operators to reduce lines of code and i am loving it. if (x==y) { z += x; } else if (x==z) { z += y; } else { z += 1; } i can do this now if there is only one if statement like this: z = x == y ? z += x : z += 1; 回答1: It would be like this: z = x == y ? z + x : x == z ? z + y : z + 1; If you use z += x as an operand it will end up doing z = (z += x) . While it works in this special case, as the result of the expression