博客
关于我
JAVA条件运算符
阅读量:347 次
发布时间:2019-03-04

本文共 415 字,大约阅读时间需要 1 分钟。

条件运算符(?:)

条件运算符也被称为三元运算符。该运算符有3个操作数,并且需要判断布尔表达式的值。其主要作用是决定哪个值应该赋值给变量。

条件运算符的语法格式为:variable x = (expression) ? value if true : value if false。

public class Test { public static void main(String[] args) { 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); } }

运行效果:根据条件判断,输出结果如图所示。

转载地址:http://szzr.baihongyu.com/

你可能感兴趣的文章
python基于pygame实现跨年烟花效果
查看>>
python基于flask搭建http服务(四)—— Docker容器化部署
查看>>
python基于flask搭建http服务(二)—— 实现Excel上传、数据清洗、入库
查看>>
python基于flask搭建http服务(三)—— 使用gunicorn部署项目
查看>>
python基于flask搭建http服务(一)—— 实现数据查询和Excel导出
查看>>
Python块键盘/鼠标输入
查看>>
Python在正确的时区获取当前时间
查看>>
python在心理学研究中的应用有哪些_心理学在线研究可用平台简介和应用进展
查看>>
python系列【仅供参考】:python tornado 集成redis消息订阅的异步任务之后tornado主程序无法启动,解决方案
查看>>
Python在列表理解中使用枚举
查看>>
python在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
查看>>
python在gpu上运行_【python】python开启GPU加速
查看>>
Python在def函数中使用for循环
查看>>