public class SopResult { public static void main(String[] args) { int i = 4;
System.out.println("The result is: " + ((i > 4) ? 9.99 : 9));
} }
读程序,输出结果:The result is: 9.0
分析可知,本程序相当于:
public class SopResult { public static void main(String[] args) { int i = 4;
double result = (i > 4) ? 9.99 : 9;
System.out.println("The result is: " + result);
} }
由于9.99为double类型,因此返回结果类型应为double类型
相当于double result = 9;
所以输出结果为The result is: 9.0