大或小
在程式裡做決策需要比較數值,判斷哪個更大、更小,或是否相等。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);
}
}