问题
This is my ternary condition in Java.
new_user_id.equals(userid) && new_key.equals(key) && !new_value.equals(value) ? updateValue() : System.out.println("New value already exists in DB");
I'm trying to match three conditions and call updateValue() function if all three conditions gets true. But still its throwing error "The left-hand side of an assignment must be a variable". Any ideas?
回答1:
JLS-15.25. Conditional Operator ? : says (in part)
It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.
回答2:
The ternary operator is used for conditional variable assignment or something like:
System.out.println("You have " + items + (items == 1 ? "item." : "items."));
Therefore, it must return something. Use an if
statement instead.
来源:https://stackoverflow.com/questions/35330842/ternary-operator-with-multiple-condtions-in-java-throwing-error