National Taiwan University Department of Computer Science and

  • Slides: 56
Download presentation
National Taiwan University Department of Computer Science and Information Engineering Language Basics Lecturer: 楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Language Basics Lecturer: 楊昌樺 1

National Taiwan University Department of Computer Science and Information Engineering Outline 變數 Variables 運算子

National Taiwan University Department of Computer Science and Information Engineering Outline 變數 Variables 運算子 Operators 運算式(Expressions)、敘述(Statements)與程式區塊( Blocks) Your Turn 程式流程 Control Flow Statements Decision making statement • if-else statements • switch statements Your Turn 2

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 定義 儲存資料的單位

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 定義 儲存資料的單位 每個變數都有各自的名稱(name)與資料型態(data type) 變數宣告(Declaration) 意義:告知電腦我的程式要使用變數,請配置記憶體以 供存放。 變數必須在第一次使用前宣告! 語法:Data. Type Var. Name [= Initial. Value]; 範例: int x = 6; double y; 3

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 基本資料型態(Primitive Data

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 基本資料型態(Primitive Data Types) 型態名稱 大小 / 格式 範圍 說明 整數型態 負數: 二進位數型態首位為 1時(11110111 → 先減 1 → 11110110 → 取補數 → 00001001 → 9 → 真正的值 → -9) byte 8 bit / 二補數方式 -128 ~ 127 位元組整數 (Byte Integer) short 16 bit / 二補數方式 -32768 ~ 32767 短整數 (Short Integer) int 32 bit / 二補數方式 -2147483648 ~ 2147483647 整數 (Integer) long 64 bit / 二補數方式 -9223372036854775808 ~ 9223372036854775807 長整數 (Long Integer) 實數型態 (以近似表示任意某個實數;a = m * be) float 32 bit / IEEE 754 格式 七位小數 單精度浮點數 double 64 bit / IEEE 754 格式 十五位小數 雙精度浮點數 其它型態 char 16 bit / Unicode 格式 u 0000 ~ uffff (0 ~ 65535) 字元 boolean true / false true, false 布林值 4

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 變數宣告完畢後可以給定初值。 給初值的方法:

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 變數宣告完畢後可以給定初值。 給初值的方法: int x; x = 7; int x = 7, y; y=x; x 記憶體 y 5

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 變數初始值(Initialization) 型態名稱

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 變數初始值(Initialization) 型態名稱 初值設定 整數型態 byte x = 3; short x = 3; int x = 3; long x = 3 L; 實數型態 float x = 1. 234 F; float x = 1. 234 e-4 F; double x = 1. 234; double x = 1. 234 D; double x = 1. 234 e-4 D; 其它型態 char x = ‘a’; boolean x = true; 6

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 溢位(Overflow) 一種當數值超過資料型態範圍的錯誤

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 溢位(Overflow) 一種當數值超過資料型態範圍的錯誤 Example: Int. Overflow. java int_i = Integer. MAX_VALUE; System. out. println("int_i= " + int_i++); 7

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 變數存取型態 基本型態(Primitive

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 變數存取型態 基本型態(Primitive Type)- 直接存取 • 整數、浮點數、字元、或布林值 參考型態(Reference Type)- 間接存取 • 由物件所組成的型態 8

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 基本型態與參考型態在記憶體中的不同 基本型態

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 基本型態與參考型態在記憶體中的不同 基本型態 參考型態 int x = 38; x 38 String s; 0 x 1234 s 0 x 5678 此變數資料 存放之位置 0 x 1234 0 x 5678 10

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 關鍵字 Java

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 關鍵字 Java 保留給本身所使用的文字,也有人稱為 “保留字” (Reserved Words) Keyword: abstract default if private this implements protected throw break throws byteelse instanceof return extends int short try catch final interface static long strictfp volatile class float native for new switch continue goto synchronized boolean do double import public transient case void char super while package finally const 12

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 常數 語法:final

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 常數 語法:final Data. Type Var. Name = Initial. Value; 一旦變數被宣告成 final,其值將不可再更改 若程式欲更改 final 變數的值將造成編譯時期錯誤( compile-time error) 範例: final double PI = 3. 1416; PI = 2. 345; // 會發生錯誤,因為 PI 是常數 13

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 變數有效範圍(Scope) 變數有效範圍

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 變數有效範圍(Scope) 變數有效範圍 = 宣告處 ~ 所屬下大括號為止 class Hello. World { int x; public static void Count (int x) { … 主 類 別 方 法 例 外 處 理 int x; catch (int x) { } } } 例外處理器參數 區域變數 方法參數 15 成員變數

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 16

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables 16

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables Example: Area.

National Taiwan University Department of Computer Science and Information Engineering 變數 Variables Example: Area. java /* 程式目的: 計算圓的面積 */ public class Area { public static void main (String args []) { final double PI = 3. 1416; //pi,近似值 double r, area; r = 10. 8; //圓的半徑 area = PI * r; //計算面積 System. out. println(“Area of circle is: ” + area); } } 17

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 算術運算子(Arithmetic Operators) 單一運算子

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 算術運算子(Arithmetic Operators) 單一運算子 用法 說明 + + X 正 X - – X 負 X 二元運算子 用法 說明 + X + Y X 加上 Y - X – Y X 減去 Y * X * Y X 乘以 Y / X / Y X 除以 Y % X % Y 取 X 除以 Y 的餘數 19

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 算術運算子(Arithmetic Operators) 單一增減運算子

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 算術運算子(Arithmetic Operators) 單一增減運算子 用法 說明 ++ (前置式) ++X X = X +1 -- (前置式) -- X X = X -1 (後置式) ++ X = X +1 (後置式) -- X = X -1 前置式:加 / 減後再取用變數 後置式:取用變數後再加 / 減 X = 3; System. out. println(X++); System. out. println(X); X = 3; System. out. println(++X); System. out. println(X) 20

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) Example: Prefix. Postfix.

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) Example: Prefix. Postfix. java x = 3; System. out. println("Before Prefix X = " + ++x); System. out. println("After Prefix X = " + x); x = 3; System. out. println("Before Postfix X = " + x++); System. out. println("After Postfix X = " + x); 21

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 指定運算子(Assignment Operators) 功能:將運算完的結果指定給另外一個變數保存起來

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 指定運算子(Assignment Operators) 功能:將運算完的結果指定給另外一個變數保存起來 指定運算子 用法 相當於 = X = Y += X += Y X = X + Y -= X -= Y X = X – Y *= X *= Y X = X * Y /= X /= Y X = X / Y %= X %= Y X = X % Y 22

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 位元運算子加上指定運算子 用法 相當於

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 位元運算子加上指定運算子 用法 相當於 &= X &= Y X = X & Y |= X |= Y X = X | Y ^= X ^= Y X = X ^ Y >>= X >>= Y X = X >> Y <<= X <<= Y X = X << Y >>>= X >>>= Y X = X >>> Y 24

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 關係運算子 Relational Operators

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 關係運算子 Relational Operators 功能:比較兩個變數的大小。比較的結果是一個布林值 (true / false) 關係運算子 用法 傳回 true 的條件 < X < Y X 小於 Y <= X <= Y X 小於等於 Y == X == Y X 等於 Y != X !=Y X 不等於 Y >= X >= Y X 大於等於 Y > X > Y X 大於 Y 25

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 條件運算子(Conditional Operators) 功能:將兩個條件合併起來的運算子,運算結果仍為布林值。

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 條件運算子(Conditional Operators) 功能:將兩個條件合併起來的運算子,運算結果仍為布林值。 條件運算子 用法 傳回 true 的條件 && X && Y X 與 Y 必須同時為 true X, Y不一定進行求值 || X || Y X 與 Y 之一為 true 即可 X, Y不一定進行求值 ! ! X X 為 false 26

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 其他運算子 其它運算子 用法

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 其他運算子 其它運算子 用法 功能 ? : X ? Y : Z 如果 X 為 true,傳回 Y 如果 X 為 false ,傳回 Z [] a[0] 用來存取陣列元素 . Object. Property 用來存取物件內的屬性或方法 (參數, …) f(X, Y, Z) 用來接收一系列的參數 (型態) (int) 3. 5 F 將某數值強制轉型 new Object 建立物件或陣列 instanceof X instanceof Y 檢查 X 是否為 Y 的個體 (Instance) 27

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) ? : 語法:op

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) ? : 語法:op 1 ? op 2 : op 3 唯一的三元運算子 簡略的 if-else 敘述 例:(x>0) ? (x-1) : (x+1) 即 if (x > 0) return x-1; else return x+1; 28

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 運算子優先權 位元 AND

National Taiwan University Department of Computer Science and Information Engineering 運算子(Operators) 運算子優先權 位元 AND & 位元 XOR ^ 位元 OR | 邏輯 AND 邏輯 OR 低 && || 條件運算子 ? : 指定運算子 = += -= *= /= %= &= ^= |= >>= <<= >>>= 30

National Taiwan University Department of Computer Science and Information Engineering Expressions、Statements & Blocks 運算式(Expressions)

National Taiwan University Department of Computer Science and Information Engineering Expressions、Statements & Blocks 運算式(Expressions) 運算子 範例 • int x = 3 * 4 – 1; • double y = -100 / x; 運算元 ambiguous vs. unambiguous • 擅用括號避免曖昧不明(ambiguous)的運算式 • 例如: x + y / 100 (x + y) / 100 x + (y / 100) 3*4 31

National Taiwan University Department of Computer Science and Information Engineering Expressions、Statements & Blocks 敘述(Statements)

National Taiwan University Department of Computer Science and Information Engineering Expressions、Statements & Blocks 敘述(Statements) 類似人們說話的語言( natural language ) 敘述由多個運算式構成一個完整的執行動作,並以分號 (; )作結尾 下列運算式可構成敘述 • • Assignment Expressions,例:a = 2; Any Use of ++ or --,例:a++; Method Calls,例:System. out. println(“Hi!”); Object Creation Expressions,例:Integer int. Object = new Integer(4); 32

National Taiwan University Department of Computer Science and Information Engineering Expressions、Statements & Blocks 敘述

National Taiwan University Department of Computer Science and Information Engineering Expressions、Statements & Blocks 敘述 Statements 除了由運算式構成,另有兩種敘述 • • Declaration Statement,例:int x = 2; Control Flow Statement 程式區塊 Blocks 由大括號包覆,且內含零個以上的敘述 例: if (a > 2) { … } 33

National Taiwan University Department of Computer Science and Information Engineering Your Turn Practice 請撰寫一程式,輸入姓名、身高、體重,您的程式會計

National Taiwan University Department of Computer Science and Information Engineering Your Turn Practice 請撰寫一程式,輸入姓名、身高、體重,您的程式會計 算出身材比例係數。 身材比例係數公式為:(體重Kg)/(身高m)2。 鍵入“java Slim. Body Larry 172 68” 印出 “Larry 的身材比例係數為 22. 9853…” 34

National Taiwan University Department of Computer Science and Information Engineering Your Turn Hint: Slim.

National Taiwan University Department of Computer Science and Information Engineering Your Turn Hint: Slim. Body. java 參數讀取範例 • String name = args[0]; • double height = Double. parse. Double(args[1]); public class Slim. Body { public static void main ( String[] args ) { … … … } } 35

National Taiwan University Department of Computer Science and Information Engineering 程式流程 循序結構 程式敘述 36

National Taiwan University Department of Computer Science and Information Engineering 程式流程 循序結構 程式敘述 36

National Taiwan University Department of Computer Science and Information Engineering 程式流程 選擇結構 如:if-else, switch

National Taiwan University Department of Computer Science and Information Engineering 程式流程 選擇結構 如:if-else, switch 條件 程式敘述 37

National Taiwan University Department of Computer Science and Information Engineering 程式流程 重複結構 前測式,如:for, while

National Taiwan University Department of Computer Science and Information Engineering 程式流程 重複結構 前測式,如:for, while false 條件 true 程式敘述 38

National Taiwan University Department of Computer Science and Information Engineering 程式流程 重複結構 後測式,如:do-while 程式敘述

National Taiwan University Department of Computer Science and Information Engineering 程式流程 重複結構 後測式,如:do-while 程式敘述 true 條件 false 程式敘述 39

National Taiwan University Department of Computer Science and Information Engineering 程式流程 呼叫方法時 控制權會轉換 …

National Taiwan University Department of Computer Science and Information Engineering 程式流程 呼叫方法時 控制權會轉換 … Rubby. start(); Lisa. start(); … public void run(){ … } 40

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements Why

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements Why do we need control flow statements? Control Flow Statements 41

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – if-else if/else statements if (expression){ 語法: statements… } else if (expression){ statements… } … else{ statements… } 使用 ? : 運算子代替簡單的 if-else 較複雜的情況,我們常使用巢狀 if-else 敘述 42

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – if-else 例如: 如果 (考試成績 < 60) 印出 “你不及格” 印出 “糟糕!” 否則 印出 “你及格了” 在 Java 中 if (Score < 60) { System. out. println (“你不及格”); System. out. println (“糟糕!”); } else { System. out. println (“你及格了”); } 43

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – if-else 結合關係運算子 功能:比較兩個變數的大小。比較的結果是一個布林值 (true / false) if (x <= y) … 關係運算子 用法 傳回 true 的條件 < X<Y X 小於 Y <= X <= Y X 小於等於 Y == X == Y X 等於 Y != X !=Y X 不等於 Y >= X >= Y X 大於等於 Y > X>Y X 大於 Y 44

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – if-else 結合條件運算子 功能:將兩個條件合併起來的運算子,運算結果仍為布 林值。 if((x<y) && (y < z)) … if((x<y) || (y < z)) … 條件運算子 用法 傳回 true 的條件 && X && Y X 與 Y 必須同時為 true X, Y不一定進行求值 || X || Y X 與 Y 之一為 true 即可 X, Y不一定進行求值 ! !X X 為 false 45

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – if-else 巢狀 if-else 定義: • if-else 之內還夾了別的 if-else 範例:以下的例子就需要用到巢狀 if-else • if 90 分以上同學印出 “Grade A” • else if 80 分以上同學印出 “Grade B” • else if 70 分以上同學印出 “Grade C” • else if 60 分以上同學印出 “Grade D” • else 60 分以下同學印出 “Grade F” 46

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – if-else Example: Grading. java public class Grading { public static void main (String[] args) { int Score = Integer. parse. Int(args[0]); if (Score >= 60) System. out. println (“pass!"); else System. out. println (“fail!"); } } 47

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – if-else Example: Age. java public class Age { public static void main (String[] args) { int x = Integer. parse. Int(args[0]); if(x>=20 && x<=29) System. out. println("二年級"); else if(x>=30 && x<=39) System. out. println("三年級"); else System. out. println("四年級"); } } 48

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – switch-case switch statement 適用場合:多重if-then-else敘述 case 文數字: • case 後方只能接字元或整數 • 如:case 1: 或 case ‘a’: • 不可填入一個範圍 break; • break 指令可以阻止程式繼續往下 執行,並跳出整個 switch-case 結 構 switch(expression){ case value 1: statements… break; case value 2: statements… break; … default: statements… break; } 49

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – switch-case Example: Switch. Demo. java switch (month) { case 1: System. out. println("January"); break; case 2: System. out. println("February"); break; case 3: System. out. println("March"); break; … case 10: System. out. println("October"); break; case 11: System. out. println("November"); break; case 12: System. out. println("December"); break; default: System. out. println("Not a month!"); break; } 50

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – switch-case Example: Get. Month. Days. java public class Get. Month. Days { public static void main (String[] args) { int Year = Integer. parse. Int(args[0]); int Month = Integer. parse. Int(args[1]); switch (Month) { case 1: case 3: 執行:java Get. Month. Days 2004 10 case 5: 結果: 31 days case 7: case 8: case 10: case 12: System. out. println("31 days"); break; 51

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements –

National Taiwan University Department of Computer Science and Information Engineering Control Flow Statements – switch-case 4: case 6: case 9: case 11: System. out. println("30 days"); break; case 2: if ( ((Year%4 == 0) && (Year%100 != 0)) || (Year%400 ==0) ) System. out. println("29 days"); else System. out. println("28 days"); break; } // end of switch } // end of main method } 52

National Taiwan University Department of Computer Science and Information Engineering Your Turn Hint: Slim.

National Taiwan University Department of Computer Science and Information Engineering Your Turn Hint: Slim. Body 2. java public class Slim. Body 2 { public static void main(String[] args) { … // 肥胖 System. out. println("You are too fat!!"); // 過重 System. out. println("You are over weight. "); // 標準體重 System. out. println("Good Shape! Keep going!"); // 過輕 System. out. println("Poor baby! Don't you eat something? "); } } 54

National Taiwan University Department of Computer Science and Information Engineering Your Turn Advanced Practice

National Taiwan University Department of Computer Science and Information Engineering Your Turn Advanced Practice 請寫出一個程式讀入三個整數,找出最大和最小的數。 例如,java Compare 25 23 67 輸出:最大 67; 最小 23 例如,java Compare 54 43 21 輸出:最大 54; 最小 21 55

National Taiwan University Department of Computer Science and Information Engineering Your Turn Hint: Compare.

National Taiwan University Department of Computer Science and Information Engineering Your Turn Hint: Compare. java 慢的版本 if(a>b && b>c) system. out. println(“Max= ”+a+” Min=”+c) else if(a>b && b<c) … 快的版本 int max, min; if(a>b){max = a, min = b; } … 56