Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.

Similar presentations


Presentation on theme: "JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever."— Presentation transcript:

1 JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作

2 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever it is."); }//main }//FirstP 註解 程式名 主程式 程式內容

3 註解及命名慣例 註解 –// 此行由此後為註解 –/* 此處為註解 */ 命名慣例 – 不能是關鍵字 (Key Words) – 通常命名 class 的時候,第一個字大寫 – 命名可以有 _ ,數字,但是名稱之中不得有空白 (space or tab) ,而且不可以數字為開頭。 – 常數的話所有字母都大寫 – 變數或是方法名稱時,請盡量選擇有意義且與內 容相關的單字

4 變數 (Variables) 變數的型態 (Type) –Byte(位元)8-bit two's complement –short (短整數)16-bit two's complement –int (整數)32-bit two's complement –long (長整數)64-bit two's complement –float (浮點數)32-bit IEEE 754 –double (實數)64-bit IEEE 754 –char (字元)16-bit Unicode character –boolean (布林值)true or false

5 變數範圍

6 變數的宣告 type variableName; –int x; type variableName = 初始值 ; –int x = 10;

7 逸出字( Escape Character )

8 Operators & Operands( 運算子 與運算元 ) Operators 指的就是運算符號,好比 + , - 。 而 Operand 指的就是被運算的東西,也就 是常數或者數字。比如說 5+7 , 5 跟 7 指 的就是 Operands ,而 + 就是 Operator 。

9 Assignment Operators (指定 運算子) 等號(=)這個運算子的意義是指派 ( Assign ),所以也稱之為 Assignment Operator (指定運算子)。 在程式中為將等號右邊的值指定給左邊 的變數,如 x = 10; 如果加上一行 x = x+10; 意思為右邊的 x 原來為 10 ,又加上 了 10 ,所以其值為 20 ,然後將 20 指定給 變數 x ,所以此時 x 的值為 20 。

10 Arithmetic Operators (算數運 算子)

11 + 的使用 X = 1 + 1; // 數字相加  X = 2; X = “1” + “1”; // 字串相加  X = “11”;

12 Casting class ArithmeticDemo3{ public static void main(String args[]) { int a = 25; int b = 10; double c = 25; double d = 10; System.out.println("a / b = " + ((double)a / (double)b) ); System.out.println("c / d = " + c / d); }//main }//ArithmeticDemo3 將整數 cast 成為 double

13 Relational Operators (邏輯運 算子)

14 Conditional Operators( 判斷運算子 )

15 其他的 Operators

16 if/else statement

17 Nested if/else statement

18 while & do/while loop ( 迴圈 )

19 Break & Continue 跳出 Loop 重做 Loop

20 Do …while loop 至少被執 行一次 是否繼續執 行的判斷

21 for loop 初始狀況 終止條件 每次增加量 被執行之程 式碼 注意為分號

22 九九乘法表範例

23 switch statement 根據不同的 狀況執行不 同片段的程 式碼 預設狀況 可使用 if…else 來 達到相同的 效果

24 Local Variable V.S. Global Variable Global Variable j Local Variable i

25 Method (方法) class FunDemo1 { public static void main(String args[]) { f1(); } // main public static void f1() { System.out.println("Inside Function f1()"); }//f1() } // FunDemo1 主程式 Method f1() 無傳入值 無傳回值

26 Method (方法) cont. public static double SquareArea(double x, double y) { double sArea = x*y; return sArea; } // SquareArea() 有傳回值,型 態為 double 有傳回值,所以需要 return 有兩個傳入值,分別 為 double, double 與主程式在同一 個 class ,使用 static 來修飾


Download ppt "JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever."

Similar presentations


Ads by Google