Java 中級
Jim White
Java Developer
條件為 true ✅ ➡ 執行動作
條件為 false ❌ ➡ 不做事
若分數達 90+ ➡ 列印 "Great job!"
若分數小於 90 ➡ 不列印
if (condition) {
// Code to run
}
若條件為 true,則執行大括號內的程式碼。
==、!=、>、<、>=、<=… // 若 score >= 90,列印 "Great job!"
if (score >= 90) {
System.out.println("Great job!");
}
int score = 94;
if (score >= 90) {
System.out.println("Excellent!");
}
if (score >= 70) {
System.out.println("Good job!");
}
if (score < 70) {
System.out.println("Try again!");
}
Excellent!
Good job!
可以使用不同條件,只要結果為 true/false。
if (score != 0){
System.out.println("Well, you got some points!");
}
if (message.equals("F")){
System.out.println("Try again!");
}
if (testResult == 100) {
System.out.println("Wow, you got it all!");
}
Java 中級