Java 程式設計入門

29  Download (0)

Full text

(1)

Java 程式設計入門

講師:陳昭源

CISE, NTU 2005/07/09

(2)

Outline

„ 變數 Variables

„ 運算子 Operators

„ 運算式(Expressions)、敘述(Statements)

& 程式區塊(Blocks)

„ 流程控制 Control Flow Statements

„ if-else statements

„ switch statements

(3)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 3

變數 Variables

„ 何謂變數?

„ 物件利用變數儲存其狀態

„ 每個變數都有各自的名稱(name)與資料型態

(data type)

„ 變數名稱:用以存取其所儲存之資料內容

„ 資料型態:決定此變數能夠儲存的資料種類

„ 變數宣告(declaration)

語法: DataType VarName [= InitialValue ];

„ 例:int xxx; char yyy = ‘a’;

„ 變數必須在第一次使用前宣告!

(4)

變數 Variables

„ 資料型別 Data Types

„

每個變數都必須要有一個資料型別

„

資料型別決定這個變數所儲存的資料類型

„

資料型別的種類

„

Primitive Data Types:此種類型的變數僅儲存相對應於資料型別所指定 的種類、大小、格式的資料

„

Reference Data Types:此種類型的變數實際上乃指向其所儲存的資料所 代表的記憶體位址(address),也就是指標形態(pointer);陣列

(array)、類別(class)、介面(interface)皆為此類型

(5)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 5

變數 Variables

„ Primitive Data Types

(6)

變數 Variables

„ 變數名稱

„ 不能是關鍵字(keyword)、布林字(true、false)

或是保留字 null

„ 命名慣例

„ 變數名稱以小寫開頭,類別名稱由大寫開頭

„ 如果名稱由不同字組成,必須將字組相連,且後面 的字開頭大寫,例:userLogin

„ 常數(constant)通常全部大寫,且使用底線(_)

分開字組,例:CURRENT_USER_NUM

(7)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 7

變數 Variables

„ final 變數

„ 語法:final DataType VarName = InitialValue ;

„ 一旦變數被宣告成 final,其值將不可再更改

„ 例:final int xxx = 0;

„ 若程式欲更改 final 變數的值將造成編譯時期錯誤

(compile-time error)

(8)

運算子 Operators

„ 運算子

„

運算子可對一個、兩個或三個運算元(operands)執行動作

„

依照所需的運算元數目,可分為

„

一元運算子(unary operator),例:a++、--a

„

二元運算子(binary operator),例:a = 2

„

三元運算子(ternary operator),例:(x>0) ? x-1 : (x+1)

„

依照運算子擺放位置,可分為

„

前置(prefix)運算子:運算子擺放在運算元之前,例:++i

„

後置(postfix)運算子:運算子擺放在運算元之後,例:i++

„

中置(infix)運算子:運算子擺放在中間,例:a + b

„

運算子執行動作後將回傳值,其值之型態視運算元而定

(9)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 9

運算子 Operators

„ 算術運算子 Arithmetic Operators

(10)

運算子 Operators

„ 運算元型態不同之運算

„ 若算術運算子進行整數(integer)與浮點數型態

(floating-point)數字之運算,最後結果將回傳浮 點數型態之數字;整數數字將在運算前被轉換成

(implicit conversion)浮點數型態

(11)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 11

運算子 Operators

„ 算術運算子之一元型態

„ x++ vs. ++x

(12)

運算子 Operators

„ 關係運算子 Relational Operators

„ 判斷左右運算元的關係,回傳 true 或 false

(13)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 13

運算子 Operators

„ 條件運算子 Conditional Operators

(14)

運算子 Operators

„ 位元位移運算子 Shift Operators

„ 位元邏輯運算子 Logical Operators

(15)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 15

運算子 Operators

„ 指定運算子 Assignment Operators

„ 基本指定運算子:=

„ 複合指定運算子

„

例:

a += 2 Æ a = a + 2 a *= 3 Æ a = a * 3

(16)

運算子 Operators

„ ? :

„ 語法:op1 ? op2 : op3

„ 唯一的三元運算子

„ 簡略的 if-else 敘述

„ 例:(x>0) ? (x-1) : (x+1) 即 if (x > 0)

return x-1;

else

return x+1;

(17)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 17

運算子 Operators

„ ( DataType )

„

語法:(

DataType

)

VarName

„

強迫型別轉換

„

例:

int x = 10;

double y = (double)x + 2;

„ 其他運算子

(18)

Expressions、Statements & Blocks

„ 運算式 Expressions

„ 由變數、運算子以及方法呼叫等所構成,主要目的 為計算結果並回傳值

„ ambiguous vs. unambiguous

„

例:

x + y / 100 (x + y) / 100 x + (y / 100)

(19)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 19

Expressions、Statements & Blocks

„ 運算子優先序

„ 擅用括號避免曖昧不

明(ambiguous)的運

算式

(20)

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 intObject = new Integer(4);

(21)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 21

Expressions、Statements & Blocks

„ 敘述 Statements

„ 除了由運算式構成,另有兩種敘述

„

Declaration Statement,例:int x = 2;

„

Control Flow Statement

„ 程式區塊 Blocks

„ 由大括號包覆,且內含零個以上的敘述

„ 例:

if (a > 2)

{ a -= 1; }

(22)

流程控制 Control Flow Statements

„ Why do we need control flow statements?

„ Control Flow Statements

(23)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 23

流程控制 Control Flow Statements

„ if/else statements

„ 語法:

if(

expression

)

{

statements…

} else if(expression)

{

statements…

}

… else

{

statements…

}

„ 使用 ? : 運算子代替簡單的 if-else

„ 較複雜的情況,我們常使用巢狀 if-else 敘述

(24)

流程控制 Control Flow Statements

„ switch Statement

„

語法:

switch(

expression

) {

case

value1

:

statements…

break;

case

value2

:

statements…

break;

default:

(25)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 25

流程控制 Control Flow Statements

„ switch Statement

„ expression 的回傳值必須表示成整數值

„

byte, short, int, char

„ 每個 case 的結尾,都必須有 break; 敘述

„ if-else statement vs. switch statement

„

if-else 可用在條件式或範圍的判斷;switch 敘述僅能判斷 單一整數值

„ ASCII Table

(26)

基本程式撰寫技巧

Basic Programming Skills

„ Readability

„ 縮排、空行

„ 註解:解釋、分隔線…

„

單行註解 //…...

„

多行註解

/**************

……..

**************/

(27)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 27

基本程式撰寫技巧

Basic Programming Skills

„ 跳脫字元 Escape Characters

„ \’:單引號

„ \”:雙引號

„ \\:反斜線

„ \b:空格

„ \n:換行

„ \t:tab 鍵

(28)

基本程式撰寫技巧

Basic Programming Skills

„ 基本輸出

„ System.out.println(…);

„ System.out.print(…);

„ 輸出多個項目

„ System.out.print(“…” + xxx + “…”);

„ System.out.println(“…” + xxx + “…”);

„ 基本輸入

„ 鍵盤輸入一個字元:ReadChar.java

„ 基本 Java 類別庫

(29)

Dept. of Computer Science & Information Engineering,

National Taiwan University July 9, 2005 Page 29

Exercise

„ Exercise01

„ 讀取使用者輸入的字元,若為大寫,則轉小寫;若 為小寫,則轉大寫

„ Exercise02

„ 讀取使用者的輸入的字元,不分大小寫

„ 若輸入為 a、e、i、o、u,請輸出「VOWEL」

„ 若輸入為 y、w,請輸出「SOMETIMES A VOWEL」

„ 若輸入為其他字母,請輸出「CONSONANT」

„ 注意:不要讓程式出現例外狀況

Figure

Updating...

References

Related subjects :