package randi;
public class Test6 {
public static void main(String[] args) {
int A = 9;
int B = 8;
int C = 7;
int max = A > B ? A > C ? A : C : B > C ? B : C;
System.out.println(max);
}
}
三目運算符為右結合性
結合性只有在相同優先級的運算符間才起作用,比如a+b*c,+與*優先級不同,這里根本不用去管結合性。而a+b+c則需要進行結合性考慮了,如果+為左結合性,那么應該理解為:(a+b)+c,如果+為右結合性,那么應該理解為a+(b+c),當然了,我們已經知道+為左結合性了。
對於?:,在C中與它優先級相同的只有它自己,因此只有連續的?:才會體現出它的右結合性,即a?b:c?d:e中,根據右結合性可知應理解為a?b:(c?d:e)。