Liang Introduction to Java Programming Ninth Edition c

  • Slides: 68
Download presentation
第三章 選擇 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc.

第三章 選擇 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1

簡介 如果在範例程式 2. 2 Compute. Area. With. Console. Input. java 裡對radius輸入負值,程式會顯示無效的結果。如果半 徑值為負的,我們不希望程式進行面積計算。那要如 何處理這樣的情況呢? Liang,

簡介 如果在範例程式 2. 2 Compute. Area. With. Console. Input. java 裡對radius輸入負值,程式會顯示無效的結果。如果半 徑值為負的,我們不希望程式進行面積計算。那要如 何處理這樣的情況呢? Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2

 boolean資料型態 於程式中我們常需要比較兩個值,比方說, 半徑值是大於 0,等於 0,或是小於 0呢?Java 提供六種比較運算子(comparison operators), 又被稱作關係運算子(relational),可用來對兩 個值做比較。比較運算的結果為一個布林值 true或false。 boolean b

boolean資料型態 於程式中我們常需要比較兩個值,比方說, 半徑值是大於 0,等於 0,或是小於 0呢?Java 提供六種比較運算子(comparison operators), 又被稱作關係運算子(relational),可用來對兩 個值做比較。比較運算的結果為一個布林值 true或false。 boolean b = (1 > 2); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4

比較運算子 (表裡假設radius為 5) Java運算子 數學符號 名稱 範例(radius是 5) 結果 < < 小於 radius <

比較運算子 (表裡假設radius為 5) Java運算子 數學符號 名稱 範例(radius是 5) 結果 < < 小於 radius < 0 false <= ≤ 小於或等於 radius <= 0 false > > 大於 radius > 0 true >= ≥ 大於或等於 radius >= 0 true == = 等於 radius == 0 false != ≠ 不等於 radius != 0 true Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5

範例程式 3. 1 Addition. Quiz. java 撰寫一支程式,讓一年級學生練習加法。此程 式會隨機產生兩個單數位的整數,number 1及 number 2,接著顯示要讓學生回答的問題,例如, “What is 1

範例程式 3. 1 Addition. Quiz. java 撰寫一支程式,讓一年級學生練習加法。此程 式會隨機產生兩個單數位的整數,number 1及 number 2,接著顯示要讓學生回答的問題,例如, “What is 1 + 7?”。學生輸入答案後,程式便會 顯示答案是對是錯的訊息。. Addition. Quiz Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Run 6

單向if敘述 if (boolean-expression) { statement(s); } if (radius >= 0) { area = radius

單向if敘述 if (boolean-expression) { statement(s); } if (radius >= 0) { area = radius * PI; System. out. println("The area" + " for the circle of radius " + radius + " is " + area); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 7

註釋 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

註釋 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8

範例程式 3. 2 Simple. If. Demo. java 撰寫一程式提示使用者輸入一個整數,如果該整數為 5的倍 數,程式便會顯示Hi. Five。如果該整數能被 2整除,程式則會 顯示Hi. Even。

範例程式 3. 2 Simple. If. Demo. java 撰寫一程式提示使用者輸入一個整數,如果該整數為 5的倍 數,程式便會顯示Hi. Five。如果該整數能被 2整除,程式則會 顯示Hi. Even。 Simple. If. Demo Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Run 9

範例程式 3. 3 Guess. Birthday. java 撰寫一個程式來猜測使用者的生日。 Guess. Birthday Liang, Introduction to Java Programming,

範例程式 3. 3 Guess. Birthday. java 撰寫一個程式來猜測使用者的生日。 Guess. Birthday Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 10

遊戲的數學基礎 19 的二進位數值是 10011。 7的二進位數值是 111。 23的二進 位數值是 11101。 Liang, Introduction to Java Programming,

遊戲的數學基礎 19 的二進位數值是 10011。 7的二進位數值是 111。 23的二進 位數值是 11101。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 11

雙向if-else敘述 if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Liang, Introduction to Java

雙向if-else敘述 if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 12

if-else敘述的範例 if (radius >= 0) { area = radius * 3. 14159; System. out.

if-else敘述的範例 if (radius >= 0) { area = radius * 3. 14159; System. out. println("The area for the “ + “circle of radius " + radius + " is " + area); } else { System. out. println("Negative input"); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 13

巢狀if敘述 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

巢狀if敘述 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 14

多向if-else敘述 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

多向if-else敘述 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 15

animation 追蹤 if-else 敘述 假設得分為 70. 0 此假設為false if (score >= 90. 0) grade

animation 追蹤 if-else 敘述 假設得分為 70. 0 此假設為false if (score >= 90. 0) grade = 'A'; else if (score >= 80. 0) grade = 'B'; else if (score >= 70. 0) grade = 'C'; else if (score >= 60. 0) grade = 'D'; else grade = 'F'; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 16

animation 追蹤 if-else 敘述 假設得分為 70. 0 此假設為false if (score >= 90. 0) grade

animation 追蹤 if-else 敘述 假設得分為 70. 0 此假設為false if (score >= 90. 0) grade = 'A'; else if (score >= 80. 0) grade = 'B'; else if (score >= 70. 0) grade = 'C'; else if (score >= 60. 0) grade = 'D'; else grade = 'F'; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 17

animation 追蹤 if-else 敘述 假設得分為 70. 0 此假設為true if (score >= 90. 0) grade

animation 追蹤 if-else 敘述 假設得分為 70. 0 此假設為true if (score >= 90. 0) grade = 'A'; else if (score >= 80. 0) grade = 'B'; else if (score >= 70. 0) grade = 'C'; else if (score >= 60. 0) grade = 'D'; else grade = 'F'; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18

animation 追蹤 if-else 敘述 假設得分為 70. 0 成績為 C if (score >= 90. 0)

animation 追蹤 if-else 敘述 假設得分為 70. 0 成績為 C if (score >= 90. 0) grade = 'A'; else if (score >= 80. 0) grade = 'B'; else if (score >= 70. 0) grade = 'C'; else if (score >= 60. 0) grade = 'D'; else grade = 'F'; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 19

animation 追蹤 if-else 敘述 假設得分為 70. 0 離開此if敘述 if (score >= 90. 0) grade

animation 追蹤 if-else 敘述 假設得分為 70. 0 離開此if敘述 if (score >= 90. 0) grade = 'A'; else if (score >= 80. 0) grade = 'B'; else if (score >= 70. 0) grade = 'C'; else if (score >= 60. 0) grade = 'D'; else grade = 'F'; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 20

註釋 else會對應同一區塊中最接近的的if敘述。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc.

註釋 else會對應同一區塊中最接近的的if敘述。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 21

註釋 如為強制else敘述對應同區塊中第一個if敘述,則應 加上“ { } ”來加以分隔其他的if敘述。 int i = 1; int j = 2;

註釋 如為強制else敘述對應同區塊中第一個if敘述,則應 加上“ { } ”來加以分隔其他的if敘述。 int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System. out. println("A"); } else System. out. println("B"); 此敘述最後會顯示出B。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22

選擇敘述常見的錯誤:將分號加在if程式列的最後 if (radius >= 0); Wrong { area = radius*PI; System. out. println( "The

選擇敘述常見的錯誤:將分號加在if程式列的最後 if (radius >= 0); Wrong { area = radius*PI; System. out. println( "The area for the circle of radius " + radius + " is " + area); } 這類錯誤既不是編譯錯誤,也不是執行期間的錯誤,而是邏 輯錯誤,因此不好偵錯。 當使用next-line style時,這類錯誤很常出現。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23

要訣 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

要訣 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 24

注意 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

注意 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25

範例程式 3. 4 Subtraction. Quiz. java 假設我們想開發一支程式,讓一年級學生 練習減法運算。此程式隨機產生兩個單數 位整數,number 1及number 2且number 1 >= number

範例程式 3. 4 Subtraction. Quiz. java 假設我們想開發一支程式,讓一年級學生 練習減法運算。此程式隨機產生兩個單數 位整數,number 1及number 2且number 1 >= number 2,接著顯示如"What is 9 – 2?"之 類的問題?當學生輸入答案後,程式會顯 示答對或答錯的訊息。 Subtraction. Quiz Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 26

範例程式 3. 5 Compute. And. Interpret. BMI. java 身高重量指標(Body Mass Index, BMI)根據身高 與體重來衡量健康狀況。計算公式為將以公斤為 單位的體重,除上以公尺為單位的身高平方。對

範例程式 3. 5 Compute. And. Interpret. BMI. java 身高重量指標(Body Mass Index, BMI)根據身高 與體重來衡量健康狀況。計算公式為將以公斤為 單位的體重,除上以公尺為單位的身高平方。對 於 20歲以上的BMI說明如下: BMI 低於 18. 5 -24. 9 25. 0 -29. 9 超過30. 0 說明 體重不足 正常 過重 肥胖 Compute. And. Interpret. BMI Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 27

範例程式 3. 6 Compute. Tax. java 撰寫一個計算個人所得稅的程式。這支程式須提示使 用者輸入報稅身分及可被徵稅之所得,接著計算應繳 稅款。輸入 0代表單身納稅人,1代表已婚共同報稅或 符合資格的鰥寡納稅人,2代表已婚分開報稅,3則代 表戶長。 下表為 2009年美國聯邦個人稅率:

範例程式 3. 6 Compute. Tax. java 撰寫一個計算個人所得稅的程式。這支程式須提示使 用者輸入報稅身分及可被徵稅之所得,接著計算應繳 稅款。輸入 0代表單身納稅人,1代表已婚共同報稅或 符合資格的鰥寡納稅人,2代表已婚分開報稅,3則代 表戶長。 下表為 2009年美國聯邦個人稅率: Marginal Tax Rate Single Married Filing Jointly or Qualifying Widow(er) Married Filing Separately Head of Household 10% $0 – $8, 350 $0 – $16, 700 $0 – $8, 350 $0 – $11, 950 15% $8, 351– $33, 950 $16, 701 – $67, 900 $8, 351 – $33, 950 $11, 951 – $45, 500 25% $33, 951 – $82, 250 $67, 901 – $137, 050 $33, 951 – $68, 525 $45, 501 – $117, 450 28% $82, 251 – $171, 550 $137, 051 – $208, 850 $68, 525 – $104, 425 $117, 451 – $190, 200 33% $171, 551 – $372, 950 $208, 851 – $372, 950 $104, 426 – $186, 475 $190, 201 - $372, 950 35% $372, 951+ $186, 476+ $372, 951+ Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 28

範例程式 3. 6 Compute. Tax. java if (status == 0) { // Compute tax

範例程式 3. 6 Compute. Tax. java if (status == 0) { // Compute tax for single filers } else if (status == 1) { // Compute tax for married file jointly // or qualifying widow(er) } else if (status == 2) { // Compute tax for married file separately } else if (status == 3) { // Compute tax for head of household } else { // Display wrong status } Compute. Tax Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 29

邏輯運算子 名稱 說明 ! not 邏輯否定(logical negation) && and 邏輯連接(logical conjunction) || or 邏輯分離(logical

邏輯運算子 名稱 說明 ! not 邏輯否定(logical negation) && and 邏輯連接(logical conjunction) || or 邏輯分離(logical disjunction) ^ exclusive or 邏輯互斥(logical exclusion) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 30

! 運算子的真值表 p true false !p 範例 (assume age = 24, gender = 'F')

! 運算子的真值表 p true false !p 範例 (assume age = 24, gender = 'F') false !(age > 18) 是 false,因為 (age > 18) 是true !(gender == 'M') 是 true,因為 (gender == 'M') 是 false Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 31

&& 運算子的真值表 p 1 p 2 p 1 && p 2 範例 (assume age

&& 運算子的真值表 p 1 p 2 p 1 && p 2 範例 (assume age = 24, gender = 'F') false (age > 18) && (gender == 'F') 是true, 因為 (age > 18) 與 (gender == 'F') 皆為true false true false (age > 18) && (gender != 'F') 是false, 因為 (gender != 'F') 是 false true Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 32

|| 運算子的真值表 p 1 p 2 p 1 || p 2 範例 (assume age

|| 運算子的真值表 p 1 p 2 p 1 || p 2 範例 (assume age = 24, gender = 'F') false (age > 34) || (gender == 'F') 是 true, 因為 (gender == 'F') 是 true false true true (age > 34) || (gender == 'M') 是 false, 因為(age > 34) 與 (gender == 'M')皆為 false Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 33

 ^ 運算子的真值表 p 1 p 2 p 1 ^ p 2 false true

^ 運算子的真值表 p 1 p 2 p 1 ^ p 2 false true false true 範例 (assume age = 24, gender = 'F') (age > 34) ^ (gender == 'F') 是 true, 因為 (age > 34) 是 false, 但(gender == 'F') 是 true (age > 34) ^ (gender == 'M') 是 false, 因為(age > 34) 與 (gender == 'M') 皆為false. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 34

範例程式 3. 7 Test. Boolean. Operators. java 此程式分別檢視輸入的數值是否能被 2且 3整除,是否 能被 2或 3整除,是否能被 2或

範例程式 3. 7 Test. Boolean. Operators. java 此程式分別檢視輸入的數值是否能被 2且 3整除,是否 能被 2或 3整除,是否能被 2或 3,但不能被兩者同時整 除: Test. Boolean. Operators Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 35

範例程式 3. 7 Test. Boolean. Operators. java System. out. println("Is " + number +

範例程式 3. 7 Test. Boolean. Operators. java System. out. println("Is " + number + " divisible by 2 and 3? " + ((number % 2 == 0) && (number % 3 == 0))); System. out. println("Is " + number + " divisible by 2 or 3? " + ((number % 2 == 0) || (number % 3 == 0))); System. out. println("Is " + number + " divisible by 2 or 3, but not both? " + Test. Boolean. Operators ((number % 2 == 0) ^ (number % 3 == 0))); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Run 36

Companion Website & 與 | 運算子 詳見補充 III. B, “The & and | Operators”

Companion Website & 與 | 運算子 詳見補充 III. B, “The & and | Operators” Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 37

Companion Website & 與 | 運算子 If x is 1, what is x after

Companion Website & 與 | 運算子 If x is 1, what is x after this expression? (x > 1) & (x++ < 10) If x is 1, what is x after this expression? (1 > x) && ( 1 > x++) How about (1 == x) | (10 > x++)? (1 == x) || (10 > x++)? Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 38

範例程式 3. 8 Leap. Year. java 此程式會先提示使用者輸入一個資料型態為int的 年數,並檢查此年是否為閏年。 A year is a leap year

範例程式 3. 8 Leap. Year. java 此程式會先提示使用者輸入一個資料型態為int的 年數,並檢查此年是否為閏年。 A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400. (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) Leap. Year Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 39

switch敘述 switch (status) { case 0: compute taxes for single filers; break; case 1:

switch敘述 switch (status) { case 0: compute taxes for single filers; break; case 1: compute taxes for married file jointly; break; case 2: compute taxes for married file separately; break; case 3: compute taxes for head of household; break; default: System. out. println("Errors: invalid status"); System. exit(1); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 41

switch敘述的流程圖 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

switch敘述的流程圖 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 42

switch敘述的規則 switch-expression必須 是char、short、int或 String型態,且必須被 封裝在括弧內。 valuel,……及value. N的型態 必須乾switcch-expression的 型態相同。 當case敘述裡的值與switcchexpression相同時,這個case 之下的敘述內便會被執行, 直到break敘述,或switch敘 述的結尾。 switch

switch敘述的規則 switch-expression必須 是char、short、int或 String型態,且必須被 封裝在括弧內。 valuel,……及value. N的型態 必須乾switcch-expression的 型態相同。 當case敘述裡的值與switcchexpression相同時,這個case 之下的敘述內便會被執行, 直到break敘述,或switch敘 述的結尾。 switch (switch-expression) { case value 1: statement(s)1; break; case value 2: statement(s)2; break; … case value. N: statement(s)N; break; default: statement(s)-for-default; } valuel,……及value. N都是常 數運算式,不帶有像 1 + x 之 類的變數。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 43

switch敘述的規則 break關鍵字雖然式可選擇 性加入,但是,每段case 敘述完畢時都應以break 來結束敘述。如果沒有以 break做結尾,|則會繼續 進行下一個case敘述。 default case 可選擇性加 入,當沒有任何case符合 switch-expression 時,便 可執行default

switch敘述的規則 break關鍵字雖然式可選擇 性加入,但是,每段case 敘述完畢時都應以break 來結束敘述。如果沒有以 break做結尾,|則會繼續 進行下一個case敘述。 default case 可選擇性加 入,當沒有任何case符合 switch-expression 時,便 可執行default case 裡的內 容。 switch (switch-expression) { case value 1: statement(s)1; break; case value 2: statement(s)2; break; … case value. N: statement(s)N; break; default: statement(s)-for-default; } 為了避免程式錯誤,以及提升程式的 可維護性,如果break刻意被省略,最 好在case敘述裡做好註解說明。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 44

animation 追蹤 switch 敘述 假設字元 ch 是 'a': switch case } (ch) 'a': 'b':

animation 追蹤 switch 敘述 假設字元 ch 是 'a': switch case } (ch) 'a': 'b': 'c': { System. out. println(ch); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 45

animation 追蹤 switch 敘述 字元ch 是 'a': switch case } (ch) 'a': 'b': 'c':

animation 追蹤 switch 敘述 字元ch 是 'a': switch case } (ch) 'a': 'b': 'c': { System. out. println(ch); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 46

animation 追蹤 switch 敘述 執行這項敘述 switch case } (ch) 'a': 'b': 'c': { System.

animation 追蹤 switch 敘述 執行這項敘述 switch case } (ch) 'a': 'b': 'c': { System. out. println(ch); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 47

animation 追蹤 switch 敘述 執行這項敘述 switch case } (ch) 'a': 'b': 'c': { System.

animation 追蹤 switch 敘述 執行這項敘述 switch case } (ch) 'a': 'b': 'c': { System. out. println(ch); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 48

animation 追蹤 switch 敘述 執行這項敘述 switch case } (ch) 'a': 'b': 'c': { System.

animation 追蹤 switch 敘述 執行這項敘述 switch case } (ch) 'a': 'b': 'c': { System. out. println(ch); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 49

animation 追蹤 switch 敘述 執行下則敘述 switch case } (ch) 'a': 'b': 'c': { System.

animation 追蹤 switch 敘述 執行下則敘述 switch case } (ch) 'a': 'b': 'c': { System. out. println(ch); Next statement; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 50

animation Trace switch statement Suppose ch is 'a': switch (ch) { case 'a': System.

animation Trace switch statement Suppose ch is 'a': switch (ch) { case 'a': System. out. println(ch); break; case 'b': System. out. println(ch); break; case 'c': System. out. println(ch); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 51

animation Trace switch statement ch is 'a': switch (ch) { case 'a': System. out.

animation Trace switch statement ch is 'a': switch (ch) { case 'a': System. out. println(ch); break; case 'b': System. out. println(ch); break; case 'c': System. out. println(ch); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 52

animation 追蹤 switch 敘述 執行這項敘述 switch (ch) { case 'a': System. out. println(ch); break;

animation 追蹤 switch 敘述 執行這項敘述 switch (ch) { case 'a': System. out. println(ch); break; case 'b': System. out. println(ch); break; case 'c': System. out. println(ch); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 53

animation 追蹤 switch 敘述 執行這項敘述 switch (ch) { case 'a': System. out. println(ch); break;

animation 追蹤 switch 敘述 執行這項敘述 switch (ch) { case 'a': System. out. println(ch); break; case 'b': System. out. println(ch); break; case 'c': System. out. println(ch); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 54

animation 追蹤 switch 敘述 執行下則敘述 switch (ch) { case 'a': System. out. println(ch); break;

animation 追蹤 switch 敘述 執行下則敘述 switch (ch) { case 'a': System. out. println(ch); break; case 'b': System. out. println(ch); break; case 'c': System. out. println(ch); } Next statement; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 55

範例程式 3. 10 Chinese. Zodiac. java 撰寫一支程式提示使用者輸入年份,並顯示該年份相 對應的十二生肖。 Chinese. Zodiac Liang, Introduction to Java

範例程式 3. 10 Chinese. Zodiac. java 撰寫一支程式提示使用者輸入年份,並顯示該年份相 對應的十二生肖。 Chinese. Zodiac Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 56

條件運算式 if (x > 0) y = 1 else y = -1; is equivalent

條件運算式 if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1; (boolean-expression) ? expression 1 : expression 2 Ternary operator Binary operator Unary operator Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 57

條件運算式 if (num % 2 == 0) System. out. println(num + “is even”); else

條件運算式 if (num % 2 == 0) System. out. println(num + “is even”); else System. out. println(num + “is odd”); System. out. println( (num % 2 == 0)? num + “is even” : num + “is odd”); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 58

條件運算式 (boolean-expression) ? exp 1 : exp 2 Liang, Introduction to Java Programming, Ninth

條件運算式 (boolean-expression) ? exp 1 : exp 2 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 59

常用的格式指示字 %b %c %d %f %e %s 輸出 布林值 字元 十進位整數 浮點數 標準科學記號表示法 字串

常用的格式指示字 %b %c %d %f %e %s 輸出 布林值 字元 十進位整數 浮點數 標準科學記號表示法 字串 範例 true或false 'a' 200 45. 460000 4. 556000 e+01 "Java is cool" Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 61

運算子優先順序圖表 運算優先順序 運算子 var++ and var-- (Postfix) +, - (Unary plus and minus), ++var

運算子優先順序圖表 運算優先順序 運算子 var++ and var-- (Postfix) +, - (Unary plus and minus), ++var and --var (Prefix) (type) (Casting) ! (Not) *, /, % (Multiplication, division, and remainder) +, - (Binary addition and subtraction) <, <=, >, >= (Comparison) ==, != (Equality) ^ (Exclusive OR) && (AND) || (OR) =, +=, -=, *=, /=, %= (Assignment operator) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 62

範例 根據優先順序及結合性規則來執行,運算式 3 + 4 * 4 > 5 * (4 + 3) -

範例 根據優先順序及結合性規則來執行,運算式 3 + 4 * 4 > 5 * (4 + 3) - 1 的解析過程如下圖所示: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 65

Companion Website 運算解析順序 詳見補充 III. A, “Advanced discussions on how an expression is evaluated

Companion Website 運算解析順序 詳見補充 III. A, “Advanced discussions on how an expression is evaluated in the JVM. ” Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 66

確認對話方塊 int option = JOption. Pane. show. Confirm. Dialog (null, "Continue"); Liang, Introduction to

確認對話方塊 int option = JOption. Pane. show. Confirm. Dialog (null, "Continue"); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 67

範例程式 3. 11 Guess. Birthday. Using. Confirmation. Dialog. java 撰寫一個程式來猜測使用者的生日。 Guess. Birthday. Using. Confirmation.

範例程式 3. 11 Guess. Birthday. Using. Confirmation. Dialog. java 撰寫一個程式來猜測使用者的生日。 Guess. Birthday. Using. Confirmation. Dialog Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 68