// 三元运算符 (Ternary Operator) 在 Java 中的用法
public class TernaryExample {
public static void main(String[] args) {
int number = 10;
String result;
// 使用三元运算符判断 number 是否大于 5
// 如果条件为 true,则返回 "Greater than 5",否则返回 "Less than or equal to 5"
result = (number > 5) ? "Greater than 5" : "Less than or equal to 5";
// 输出结果
System.out.println(result);
}
}
condition ? value_if_true : value_if_false(number > 5) 是条件,如果条件为 true,则返回 "Greater than 5",否则返回 "Less than or equal to 5"。if-else 更加简洁,适合用于简单的条件判断。上一篇:foreach java
下一篇:java string转日期
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站