更大还是更小
在代码中做出决策需要比较数值,判断哪个更大、更小,或是否相等。Java 提供的比较运算符会返回 true 或 false,是程序逻辑的基础。让我们来练习比较数字吧!
本练习是课程的一部分
Java 入门
练习说明
- 将判断
5是否小于7的结果保存到boolean变量first。 - 将判断
9是否大于或等于2的结果保存到变量second。
交互式实操练习
通过完成这段示例代码来试试这个练习。
class Comparisons {
public static void main(String[] args) {
// Check if 5 is smaller than 7
boolean first = 5 ____ 7;
// Check if 9 is greater than or equal to 2
boolean second = ____ ____ ____;
System.out.println("5 is smaller than 7: " + first);
System.out.println("9 is greater or equal to 2: " + second);
}
}