Java總結十五:if-else條件判斷結構
1. 格式
格式1:
if(條件表達式){
語句塊;
}
格式2:"二選一"
if(條件表達式) {
語句塊1;
}else{
語句塊2;
}
格式3:"多選一"
if (條件表達式1) {
語句塊1;
} else if (條件表達式2) {
語句塊2;
}
...
}else if (條件表達式n) {
語句塊n;
} else {
語句塊n+1;
}
class IfElseTest {
public static void main(String[] args) {
/*
案例1:成年人心率的正常範圍是每分鐘60-100次。體檢時,
如果心率不在此範圍内,則提示需要做進一步的檢查。
*/
int heartBeats = 89;
//錯誤的寫法:if(60 <= heartBeats <= 100){
if(heartBeats < 60 || heartBeats > 100){
System.out.println("你需要做進一步的檢查");
}
System.out.println("體檢結束");
//**********************************
/*
案例2:定義一個整數,判定是偶數還是奇數
*/
int num = 13;
if(num % 2 == 0){
System.out.println(num + "是偶數");
}else{
System.out.println(num + "是奇數");
}
}
}
#java#