语法格式:
复制代码
1x?y:z
其中x为Boolean类型表达式,先计算x的值,若为true,则整个运算的结果为表达式y的值,否则整个运算结果为表达式z的值。
示例代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12public class sanmu { public static void main(String[] args) { // TODO Auto-generated method stub int score=80; int x=-100; String type=score<60?"不及格":"及格"; int flag =x>0?1:(x==0?0:-1); System.out.println("type="+type); System.out.println("flag="+flag); } }
结果:
复制代码
1
2
3type=及格 flag=-1
最后
以上就是暴躁小熊猫最近收集整理的关于java中的条件运算符三目运算的全部内容,更多相关java中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复