博客
关于我
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/

你可能感兴趣的文章
Python3 - 在字符串格式化程序参数中使用变量
查看>>
Python3 A*寻路算法实现
查看>>
python3 grpc安装
查看>>
python3 mysql 封装多表_python3 mysql-多表查询
查看>>
python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
查看>>
Python关键代码总览!从Zero到Hero必看的考点
查看>>
python3 robotframework+pycharm框架搭建
查看>>
Python3 shutil模块
查看>>
Python关于strftime函数详细解析 附实战代码
查看>>
Python3 tutorial day3
查看>>
python3 week4
查看>>
Python3 创建数组
查看>>
python3 套接字异常(a byte-like object is required not str)
查看>>
python3 实现用户三次输入错误记录黑名单并对该用户登录锁定-day1
查看>>
python3 文本变图片
查看>>
Python3 查看 com 组件的属性
查看>>
python3 案例分享--发邮件
查看>>
python3 模板库_Python3 标准库概览_Python教程_我爱模板网 - 提供下载各种免费建站资源,免费网站模板,免费网页特效,让你爱上建站!...
查看>>
Python3 求最大/小值及索引值 Numpy
查看>>
python3+django使用celery执行某些任务失败的解决方案
查看>>