說明: 一、 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);
  }

返回[JAVA]流程控制語法目錄

文章標籤
全站熱搜
創作者介紹
創作者 PG Levin Li 的頭像
PG Levin Li

程式開發學習之路

PG Levin Li 發表在 痞客邦 留言(0) 人氣(107)