Professional Documents
Culture Documents
”
1
1. 簡介
面臨抉擇情況:選擇結構(Selection)
if ( )
{
}
(2) if-else 敘述
if ( )
{
}
else
{
}
(3) 多項 if-else 敘述
if ( )
{
}
else if ( )
1
{
}
else if ( )
2
{
}
else
{
2 }
(4) switch 敘述
switch ( )
{
case 1:
case 2:
default:
}
if 流程圖
(a)
2
if (boolean-expression) {
}
statement(s);
3
(b)
if (radius >= 0) {
area = radius * radius * Math.PI;
3 System.out.println("The area" + " for the circle of radius " + radius + " is " + area);
}
if-else 流程圖
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
switch 流程圖
3
4
4
<
<=
>
>=
!= 不等於
= = 等於
範例
運算子 名稱 結果
int i=5;
4
>
>=
是否大於
是否大於等於
i > 10
i >= 5
false
true
5
i == 8 false
== 是否等於
i == 5 true
i != 8 true
!= 是否不等於
i != 5 false
5
注意:字串內容的比對必須用 equals,不要用==
使用情境 1:
String name = new String("Albert");
比較 name 內容是否與某個字串相同:
name.equals("Albert") 結果為何? true
name.equals("Tom") 結果為何? false
使用情境 2:
String colledge1 = new String("高科大");
String colledge2 = new String("第一科大");
比較兩個字串物件之內容是否相同:
colledge1.equals(colledge2) 結果為何? false
if else ?
另一種簡潔 if 的寫法,用在運算式中頗方便。
寫法:
(條件)?是值:否值
邏輯條件?條件真時的返回值:條件假時的返回值
int a = 2;
int b = 5;
5
int c;
6
System.out.println( age>70? "符合": "不符合" );
範例:列印成績及格
成績大於等於 60,列印及格
ScorePass
int score = 95;
if (score >= 60)
{
System.out.println("及格!");
範例:顯示成績是否及格
成績大於等於 60,列印及格
成績低於 60,列印不及格
ScorePassFail
6
int score = 95;
{
7
System.out.println("及格!"); //只有執行一行,大括弧可以省略
else
{
System.out.println("不及格!");
7
}
else
System.out.println("不及格!");
以下這種寫法,執行結果有沒有一樣?
int score = 95;
if (score >= 60)
{
System.out.println("及格!");
}
else if (score < 60)
{
System.out.println("不及格!");
}
習題:偶數或是奇數
判斷是偶數或是奇數
習題:成績等第列印
成績等第列印 100 分以上 零分以下 需印出成績錯誤訊息。
7
8
範例: 計算成績等第並顯示
ScoreGrade
//寫法 1
if (score >= 90)
System.out.print("A");
else if (score >= 80)
System.out.print("B");
else if (score >= 70)
System.out.print("C");
else if (score >= 60)
System.out.print("D");
else
System.out.print("F");
//寫法 2 你認為哪一種寫法比較好?
String grade;
8
if (score >= 90)
grade ="A";
else if (score >= 80)
9
grade ="B";
else if (score >= 70)
grade ="B";
else if (score >= 60)
9 grade ="D";
else
grade ="F";
System.out.println("等第:"+grade);
這樣寫也容易理解(先判斷不及格 F)
ScoreGrade2
if (score < 60)
grade ="F";
else if (score < 70)
grade ="D";
else if (score < 80)
grade ="B";
else if (score < 90)
grade ="C";
else if (score < 100)
grade ="A";
9
BMI 說明
10
BMI < 18.5 過輕
30.0 ≤ BMI 肥胖
10
參考寫法:
Bmi
if (bmi< 18.5)
suggestion ="過輕";
else if (bmi < 25)
suggestion ="正常";
else if (bmi < 30)
suggestion ="過重";
else
suggestion ="肥胖";
針對 整數 字串 字元之情況的判斷,可以採用 switch。
使用 if else if …
public class SeasonEncode {
10
String season="";
if ( season_id == 1)
11
{
season="春天";
System.out.println("百花盛開");
}
11 else if ( season_id == 2 )
{
season="夏天";
System.out.println("艷陽高照");
}
else if ( season_id == 3 )
{
season="秋天";
System.out.println("楓紅片片");
}
else if ( season_id == 4 )
{
season="冬天";
System.out.println("冷風颼颼");
}
else
{
season="錯誤代碼";
System.out.println("季節代碼錯誤!");
}
System.out.println("季節是:"+season);
} //main()
} // class Ex04
11
使用 switch 比較容易閱讀:
public class SeansonEncodeSwitch {
12
public static void main(String[] args) {
int season_id = 1;
String season = "";
12 switch (season_id) {
case 1:
season = "春天";
System.out.println("百花盛開");
break;
case 2:
season = "夏天";
System.out.println("艷陽高照");
break;
case 3:
season = "秋天";
System.out.println("楓紅片片.");
break;
case 4:
season = "冬天";
System.out.println("冷風颼颼");
break;
default:
season="不正確";
System.out.println("輸入錯誤!");
break;
}
System.out.println("季節是:" + season);
12
}
}
13
範例: 將春夏秋冬轉成 1 2 3 4 編碼
使用 switch 將春夏秋冬轉成 1 2 3 4 編碼
13
int season_id = 0;
String season="春";
switch( season )
{
case "春" :
season_id =1;
break;
case "夏":
season_id =2;
break;
case "秋":
season_id =3;
break;
case "冬":
season_id =4;
break;
default:
System.out.println("不正確");
season_id = -1;
break;
}
if (season_id!= -1)
System.out.printf("%s 轉成%d\n",season, season_id);
13
範例: 計算成績等第並顯示—使用 switch( ) 14
ABCDF 等第計算
public class ScoreGradeSwitch {
14
int score = 95;
String grade="";
System.out.printf("%d", score/10);
switch ( score/10 )
{
case 10:
System.out.println("Excellent!");
case 9:
grade="A";
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
grade="F";
break;
}
System.out.println("等第:"+grade);
}
}
14
範例: 輸入月份,列印天數—使用 switch( )
只針對平年,閏年尚未處理
15
public class DaysOfMonth {
15 int month = 1;
int days=0;
switch ( month )
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
days=28; //這裡要是否判斷閏年,若為閏年則有 29 天
break;
default:
System.out.println("異常月份");
break;
}
15
}
}
System.out.println("天數:"+days);
16
習題: 找出最大值
16
輸入三個數字找出最大值
int n1 = 2;
int n2 = 4;
int n3 = 5;
寫法 1: 只要幾行就搞定了!!
int max = n1;
if (n2 >= max) max = n2;
if (n3 >= max) max = n3;
System.out.println("最大值:" + max);
寫法 3: nested 巢狀 if (太複雜啦!)
16
}
}
max = n3;
17
else {
if (n2 >= n3) {
max = n2;
}
17 else {
max = n3;
}
}
System.out.println("最大值:" + max);
結論:
演算法 好的演算法
資料結構 好的資料結構
習題: 3 個數字排序
有三個數字 a,b,c,其數值為任意整數,請將最小值放到 a, 次小值放到 b,最大
值放到 c 存放,最後印出 a,b,c 的值。
例如:
int a = 10;
int b = 3;
int c = 7;
做以下三次比較,印出 a, b, c 的內容,就是從小排到大了。
若 a>b 交換 a,b
17
若 b>c 交換 b,c
若 a>b 交換 a,b
18
不信?你試試看就知道。
18
兩個數字交換怎麼做?
有 2 個數字 a,b,請將其值交換,最後印出來。
int a = 5;
int b = 3;
int temp;
//3 行搞定交換
temp = a;
a = b;
b = temp;
if (a > b) {
temp = a;
a = b;
b = temp;
}
18
if (b > c) {
temp = b;
b = c;
19
c = temp;
}
if (a > b) {
temp = a;
19 a = b;
b = temp;
}
System.out.printf("%d,%d,%d\n", a, b, c);
}
}
運算子 名稱 說明
|| or 邏輯分離(logical disjunction)
19
真值表:
true&&true → true
20
true&&false → false
真&&真 真
真&&假 假
20 假&&真 假
假&&假 假
true||false → true
真||真 真
真||假 真
假||真 真
假||假 假
!(邏輯否定)
boolean b = true;
!b → false
&與|運算子
多個條件 short-cut 簡捷式: &與|運算子
&與|運算子 簡潔 short-cut
有多個條件,若第一個條件不符合,後面就不檢查,直接跳離該運算式。
若是左邊的條件不成立,右邊的條件就不檢查了 (shortcut)
例如:
if ( (gender == 1) & (age >= 65) )
gender 若是否等於 1,則不會再去評估 age >= 65 這個式子
20
if ( (gender == 1) && (age >= 65) )
無論 gender 是否等於 1,都會去評估 age >= 65 這個式子
21
&& || 左、右兩邊的條件都會被執行運算與檢查
21
多條件範例
範例: 少年
若年齡介於 15(含)與 18(含)之間印出"少年",布林運算式該如何寫?
範例: 敬老卡
性別男性年紀大於 65 歲 發給 藍色敬老卡
性別女性年紀大於 68 歲 發給 粉紅色敬老卡
gender 可用 0,1 代表
int age = 76;
int gender=1;
21
範例: 可申請補助檢康檢查 22
符合條件為性別為男性 male,且年齡 75 歲以上,可申請補助檢康檢查。
int age = 76;
boolean male = true;
另一種寫法
int age = 76;
int gender = 1; //1 表示男性
範例:布林運算結果測試
假設 x=1 以下運算結果為何?
true&&true
true && (3 > 4)
(x>0) && (x <5)
(x>0) && (x<0)
(x<5) &&(x!=1)
22
(x <= 5)||(x>=10)
(x <= -5)||(x>=5)
23
範例: 被 2 整除與被 3 整除
23
⚫ 同時可被 2 整除與被 3 整除
int number = 7;
if (number % 2 == 0 && number % 3 == 0) {
System.out.println("同時可被 2 整除與被 3 整除");
}
⚫ 可被 2 整除,但不可被 3 整除
int number = 8;
if (number % 2 == 0 && number % 3 != 0) {
System.out.println("可被 2 整除,但不可被 3 整除");
}
⚫ 可被 2 整除或被 3 整除
if (number % 2 == 0 || number % 3 == 0) {
System.out.println("可被 2 整除或被 3 整除");
}
23
範例: 閏年 leap year 24
提示使用者輸入一個資料型態為 int 的年數,並檢查此年是否為閏年。
24
可被 4 整除、但不被 100 整除
1600 這是閏年喔!
1700
1800
1900
2000 這是閏年喔!
2100
2004
2008
2012
2016
2020
2021
2022
再加上一個條件,算是將被前一條件排除的納入:
➔或可被 400 整除 (可被 4 整除、也被 100 整除)
1600
2000
2400
2800
請參考課本答案
24
//寫法 1
if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 )
25
//寫法 2 這樣是否比較容易理解?
if ( year % 400 == 0) //一定要先寫這個條件,為甚麼?
{
25 System.out.println("這是閏年");
}
else if (year % 4 == 0 && year % 100 != 0)
{
System.out.println("這也是閏年");
}
習題: 可被 5 與 7 整除?
輸入一個整數,判斷以下 A B C D 四種情況:
A:同時可被 5 整除與被 7 整除
B:可被 5 整除但不可被 7 整除
C:可被 7 整除但不可被 5 整除
D:不可被 5 整除與不可被 7 整除
25
} else if (n % 5 == 0 && n % 7 != 0) {
System.out.println("可被 5 整除但不可被 7 整除");
} else if (n % 5 != 0 && n % 7 == 0) {
26
System.out.println("可被 7 整除但不可被 5 整除");
} else if (n % 5 != 0 && n % 7 != 0) {
System.out.println("不可被 5 整除與不可被 7 整除");
}
26 }
}
範例: 迪摩根定理
不能被 2 整除,也不能被 3 整除,以下寫法有何不同? 結果一樣嗎?
搞得好糊塗喔!
寫法 1:
(number % 2 != 0) && (number % 3 != 0)
寫法 2:
if ( !(number % 2 == 0) && !(number % 3 == 0) ) {
System.out.println("不能被 2 整除 也不能被 3 整除");
}
寫法 3: 迪摩根定理的運用
!(number % 2 == 0 || number % 3 == 0)
迪摩根定理
!( condition1 || condition2)
等同於
!condition1 && !condition2
範例
不可被 2 整除,或不可被 3 整除
26
寫法 1:
if ( number % 2 != 0 || number % 3 != 0 ){
27
System.out.println("不能被 2 整除 或 不能被 3 整除");
}
寫法 2: 正確嗎?請跑一下程式碼試試看
範例
可被 2 整除但不能被 3 整除,或是可被 3 整除但不能被 2 整
2, 3, 4, 5, 6, 7 8, 9, 10, 11, 12 , 13 , 14, 15, 16, 17, 18, 19, 20, 21
if (number % 2 == 0 ^ number % 3 == 0) {
System.out.println("可被 2 整除但不能被 3 整除,或是可被 3 整除但不能被 2 整");
}
4. if 巢狀結構(nested)
if 指令組之內,可以包含有 if 指令組。
if ()
{ //注意:若有多行指令需使用大括弧
//指令
if ()
{
27
}
//指令
//指令
28
}
28
範例: 小於 10 之偶數
n 小於 10 時(含),若為偶數,印出是"n 是小於 10 的偶數"。n 大於 10 時,若為
奇數,印出是"n 是大於 10 的奇數"
EvenOrNot
n=15;
if (n <= 10)
{
if (n%2 ==0)
{
System.out.println("n 是小於 10 的偶數");
}
} else if (n > 10){
if (n%2 !=0)
{
System.out.println("n 是大於 10 的奇數");
}
}
範例
檢查輸入數字是否介於 5~10 之間(包含),以下幾種寫法有何不同?
(1)
if ( num >= 5 ) // 外面 if
28
if ( num <= 10 ) // 裡面 if
29 (3)
if ( num >= 5 & num <= 10 )
System.out.println( "num is between 5 and 10" ) ;
29