1 / 23

第六章 IF 條件分支

第六章 IF 條件分支. 數學函數. if 條件式. 數學函數. 在 Java 程式語言中 , 數學函數包含於 Math 類別中,所以使用時必須在函數之前加 Math 類別名稱. 次方與開方函數. double dNum1 = Math.pow(2,3); double dNum2 = Math.sqrt(4);. 假設本金為 1 百萬,輸入年利率,以 20 年為期限,計算並輸出其本利和共為多少錢? 本利和 = 本金* (1.0+ 利率 ) 期數. public class test {

derick
Download Presentation

第六章 IF 條件分支

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第六章 IF 條件分支 數學函數 if 條件式

  2. 數學函數 在 Java 程式語言中,數學函數包含於Math類別中,所以使用時必須在函數之前加Math類別名稱 次方與開方函數 double dNum1 = Math.pow(2,3); double dNum2 = Math.sqrt(4);

  3. 假設本金為1百萬,輸入年利率,以20年為期限,計算並輸出其本利和共為多少錢?假設本金為1百萬,輸入年利率,以20年為期限,計算並輸出其本利和共為多少錢? 本利和 = 本金*(1.0+利率) 期數 public class test { public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader keyin; keyin = new java.io.BufferedReader( new java.io.InputStreamReader(System.in)); System.out.print(“請輸入年利率: "); float fNum = Float.parseFloat(keyin.readLine()); float fMoney = (float)Math.pow((1.0+fNum),20.0); System.out.println(“本利和共: ” + fMoney+”百萬元”); } }

  4. 取亂數 double a = Math.random(); //a=亂數 System.out.println("a = " + a); //輸出亂數

  5. 輸出亂數,其範圍為 1~42。 int a =(int)( Math.random()*42+1); //a=亂數 System.out.println("a = " + a); //輸出亂數

  6. 上機演練 請列印出您的班級座號以及姓名。 請產生 3 個範圍 1~3 的數字。

  7. 流程控制 何謂流程控制? 以一天的生活為例, 『早上起床後, 會先刷牙洗臉, 接著吃完早餐後就出門上課, 上完了早上的三堂課, 在餐廳吃自助餐, 午休後繼續上下午的課, 下課後跟同學相約去外面的小吃店用餐, 晚上回宿舍唸書, 最後上床睡覺』,結束一天的流程。 在 Java 程式語言中,程式的執行就如同平常的生活一樣, 是有順序性地在執行, 整個執行的順序與過程, 就是流程。

  8. 流程控制 何謂流程控制? 但是流程並非僅僅依序進行, 它可能會因為一些狀況而變化。例如下午老師請假沒來上課, 下午的課就會取消, 因而更改流程。 對於程式執行的流程順序以及因應不同狀況而選取不同的流程, 即為流程控制。

  9. if:『如果』 的意思。會根據條件運算式的結果, 來判斷是否執行敘述中的程式。如果條件運算式的結果為 true, 則執行區塊內的敘述;如果結果為 false, 則跳過區塊。 • 條件式:運算結果為布林型別的運算式, 通常由比較運算或邏輯運算所組成。 • 敘述:條件運算式結果為 true 時所要執行的動作。如果只有單一敘述, 則可以省略大括號。 複合陳述句 (Compound statement) 流程控制 if 單向條件式 if(條件式)陳述句一; if(條件式) { 陳述句一; 陳述句二; }

  10. if 單向條件式 public class IfDemo { public static void main(String[ ] args) { int a=200; int b=100; if ( a > b ) { System.out.println(“a>b"); } System.out.println(“if比對完畢”); } } 200>100 結果為true a>b if比對完畢

  11. if 單向條件式 public class IfDemo { public static void main(String[ ] args) { int a=100; int b=500; if ( a > b ) { System.out.println(“a>b"); } System.out.println(“if比對完畢”); } } 100>500 結果為false if比對完畢

  12. 使用 if 判斷汽車是否該加油的程式。油量小於2 公升時,顯示 油量不足。 import java.io.*; public class BufferedReaderDemo { public static void main(String[ ] args) throws IOException { BufferedReader keyin = new BufferedReader( new InputStreamReader(System.in)); System.out.print(“請輸入目前所剩油料 (幾公升?) : "); float liter = Float.parseFloat( keyin.readLine()); if ( ) System.out.println(“油量不足,該加油了 !"); System.out.println(“祝您行車愉快"); } } liter < 2 請輸入目前所剩油料 (幾公升?) :10 祝您行車愉快

  13. 使用 if 判斷汽車是否該加油的程式。油量小於2 公升時,顯示 油量不足。 import java.io.*; public class BufferedReaderDemo { public static void main(String[ ] args) throws IOException { BufferedReader keyin = new BufferedReader( new InputStreamReader(System.in)); System.out.print(“請輸入目前所剩油料 (幾公升?) : "); float liter = Float.parseFloat( keyin.readLine()); if ( ) System.out.println(“油量不足,該加油了 !"); System.out.println(“祝您行車愉快"); } } liter < 2 請輸入目前所剩油料 (幾公升?) :1 油量不足,該加油了 ! 祝您行車愉快

  14. 上機演練 產生一個亂數其範圍為 1~3,若產生的亂數為 1,在畫面上顯示剪刀,若產生的亂數為 2,在畫面上顯示石頭,若產生的亂數為 3,在畫面上顯示布。

  15. if 與條件位元運算子合併運算 public class IfDemo { public static void main(String[ ] args) { int a=50; if ( a > 59 ) { System.out.println(“及格"); } if ( a<60 && a > 39 ) { System.out.println(“補考"); } if ( a < 40 ) { System.out.println(“死當"); } } }

  16. 上機演練 接續上 一個上機練習,從鍵盤讀入資料,玩家可以輸入1~3來跟電腦猜拳,並判斷勝負結果。

  17. true 敘述句1 判斷條件 1 敘述主體 判斷條件 false 敘述句2 2 敘述主體 其它敘述 其他敘述 流程控制 if…else 雙向條件式 • 雙向分支(if... else...) • 語法: if (布林表示式) 敘述句1 else 敘述句2 • 例如: if ( grade >= 60 ) System.out.println("成績有達 60 分"); //用單行 else System.out.println("成績不到 60 分");

  18. 流程控制 if…else 雙向條件式 if(條件式) { 陳述句一; // 其它陳述句 } else { 陳述句二; // 其它陳述句 }

  19. if 雙向條件式 public class IfDemo { public static void main(String[ ] args) { int a=200; int b=100; if ( a > b ) { System.out.println(“a>b"); } else { System.out.println(“b>a"); } System.out.println(“if比對完畢”); } } 200>100 結果為true a>b if比對完畢

  20. if 雙向條件式 public class IfDemo { public static void main(String[ ] args) { int a=20; int b=100; if ( a > b ) { System.out.println(“a>b"); } else { System.out.println(“b>a"); } System.out.println(“if比對完畢”); } } 20>100 結果為false b>a if比對完畢

  21. 上機演練 使用者輸入身高,如果身高高於150公分,在畫面上顯示請購買全票, 反之則顯示可以購買半票。 請輸入您的身高:172.3 請購買全票!!

  22. 上機演練 徵婚啟事徵求適婚男性條件如下:年齡:25 ~ 35、身高(公分):169.5 ~ 180.4、體重(公斤):64.5 ~ 75.4,女性條件如下:年齡:20 ~ 30、身高(公分):149.5 ~ 170.4、體重(公斤):44.5 ~65.4。 請設計程式可以分別讀取鍵盤輸入的應徵者資料:性別、年齡、身高及體重,並藉此比對全部條件-合格者顯示合格,否則顯示不合格。

  23. Home Work 試寫一個電信計費程式:使用者可以輸入撥打分鐘數來計算其通話費每個月打 800 分鐘以下(不含),每分鐘 0.9 :800 ~ 1500分鐘,每分鐘 0.81元 :1500分鐘 以上(含),每分鐘 0.72元, 並於畫面上輸出其通話費。 試寫一個 MVP 計算程式:使用者可以輸入籃球員的得分,籃板,助功,抄截, 失誤等數值。並依 ( 得分*1+籃板*2+助功*2+抄截*2 - 失誤*2) 的公式取得球員之 MVP 分數。大於 45 分以上為 A 級球員,大於 25~44 分為 B 級球員,10~24為 C 級球員,低於 9 分為萬年板凳球員。將結果顯示在畫面上。

More Related