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 中级