說明: 一、 if(true){ //true 條件成立 進入此程式區塊 } 二、 if(true){ //true 條件成立 進入此程式區塊 }else{ //false 條件不成立 進入此程式區塊 } 三、 if( 條件判斷) { 條件判斷為true }else{ 條件判斷為false } 例:
// if() {} Test 1 -- 條件成立
int score = 80;
if (score >= 70) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 70:" + score);
}
// if() {} Test 2 -- 條件不成立
score = 50;
if (score >= 70) {// 不成立 條件 不進入此程式區塊
System.out.println("條件成立 score >= 70:" + score);
}
// 不成立 條件 會跳過上述程式區塊
// if() {} else {} Test 1 -- 條件成立
score = 60;
if (score >= 60) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 60:" + score);
}
else {// 條件不成立 進入此程式區塊
System.out.println("不條件成立 score:" + score);
}
// if() {} else {} Test 2 -- 條件不成立
score = 59;
if (score >= 60) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 60:" + score);
}
else {// 條件不成立 進入此程式區塊
System.out.println("不條件成立 score:" + score);
}
// if() {} else if() {} else {} Test
score = 59;
if (score >= 60) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 60:" + score);
}
else if (score == 59) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score == 59:" + score);
}
else { // 條件都不成立 進入此程式區塊
System.out.println("不條件成立 score:" + score);
}
// if() {} else if() {} else {} 可以很多個else if() Test
score = 70;
if (score >= 90) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 90:" + score);
}
else if (score >= 80) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 80:" + score);
}
else if (score >= 70) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 70:" + score);
}
else if (score >= 60) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 60:" + score);
}
else if (score >= 50) {// 條件成立 進入此程式區塊
System.out.println("條件成立 score >= 50:" + score);
}
else { // 所有條件都不成立 進入此程式區塊
System.out.println("不條件成立 score:" + score);
}
文章標籤
全站熱搜
