Ex 211 Java 01 class ProgramName 02 03

  • Slides: 87
Download presentation

Ex 2_1_1 Java 程式的基本架構 01. class Program-Name //程式名 02. { 03. // 程式內容(Program-Body) 04.

Ex 2_1_1 Java 程式的基本架構 01. class Program-Name //程式名 02. { 03. // 程式內容(Program-Body) 04. } 4 [ Java 2語言實務 ]

流程與資料之範例 01 class Demo. Arch{ 02 public static void main(String args[]) { 03 int

流程與資料之範例 01 class Demo. Arch{ 02 public static void main(String args[]) { 03 int Payrate=1385; // 作時薪 04 final int Hours=24; // 作時數 05 int Payment; //給付薪資 06 Payment = Payrate*Hours; 07 System. out. println("My payment is " + Payment + " dallors. "); 08 // 在螢幕上顯現出內容 09 } 10 } 【執行結果】 My payment is 33240 dallors. 2 -1程式、程式語言、程式架構 7 [ Java 2語言實務 ]

Ex 2_1_3 總學分費的範例 01. class Demo. Arch 2{ 02. public static void main(String args[]) {

Ex 2_1_3 總學分費的範例 01. class Demo. Arch 2{ 02. public static void main(String args[]) { 03. int Pay. School. Score=3000;  //學分費 04. int Number=24;  //學分數 05. int Payment; //總學分費 06. Payment = Pay. School. Score*Number; 07. System. out. println("我的總學分費共新台幣 " +Payment + " 元"); 08. // 在螢幕上顯現出內容 09. } 10. } 9 [ Java 2語言實務 ]

保留字 • 表 2-1、Java程式所使用的保留字 double int strictfp do boolean else interface super while break

保留字 • 表 2-1、Java程式所使用的保留字 double int strictfp do boolean else interface super while break extends long switch volatile byte final native synchronized short case finally new this static catch float package throw import char for private throws instanceof class goto protected transient default const if public try continue implements return void abstract 2 -2、變數與資料型態 14 [ Java 2語言實務 ]

基本資料型態表 基本資料型態名稱 佔用記憶體長度 最小值 最大值 預設值 整數(integer) byte(位元組) 8 -bit -128 +127 (byte)0 short(短整數)

基本資料型態表 基本資料型態名稱 佔用記憶體長度 最小值 最大值 預設值 整數(integer) byte(位元組) 8 -bit -128 +127 (byte)0 short(短整數) 16 -bit -2 15 +2 15 – 1 (short)0 int(整數) 32 -bit -2 31 +2 31 – 1 0 long(長整數) 64 -bit -2 63 +2 63 – 1 0 L 實數(real number) float(浮點數) 32 -bit IEEE 754 0. 0 f double(倍精數) 64 -bit IEEE 754 0. 0 d 其他(others) char(字元) 16 -bit ‘u 0000’ ‘u. FFFF’ ‘u 0000’(null) boolean(布林變數) 1 -bit – – false 2 -2、變數與資料型態 18 [ Java 2語言實務 ]

Ex 2_2_1 Java基本資料型態範例 class Demo. Max. Variables { public static void main(String args[]) {

Ex 2_2_1 Java基本資料型態範例 class Demo. Max. Variables { public static void main(String args[]) { // integers byte largest. Byte = Byte. MAX_VALUE; //位元最大值 short largest. Short = Short. MAX_VALUE; //短整數最大值 int largest. Integer = Integer. MAX_VALUE; //整數最大值 long largest. Long = Long. MAX_VALUE; //長整數最大值 // real numbers float largest. Float = Float. MAX_VALUE; //浮點數最大值 double largest. Double = Double. MAX_VALUE; //倍精數最大值 // other primitive types boolean a. Boolean = true; //布林運算值為真 // display them all System. out. println("The largest byte value is " + largest. Byte); System. out. println("The largest short value is " + largest. Short); System. out. println("The largest integer value is " + largest. Integer); System. out. println("The largest long value is " + largest. Long); System. out. println("The largest float value is " + largest. Float); System. out. println("The largest double value is " + largest. Double); System. out. println("The value of a. Boolean is " + a. Boolean); } } 2 -2、變數與資料型態 22 [ Java 2語言實務 ]

Java基本資料型態範例結果 【執行結果】 The The largest byte value is 127 largest short value is 32767

Java基本資料型態範例結果 【執行結果】 The The largest byte value is 127 largest short value is 32767 largest integer value is 2147483647 largest long value is 9223372036854775807 largest float value is 3. 4028235 E 38 largest double value is 1. 7976931348623157 E 308 value of a. Boolean is true 2 -2、變數與資料型態 23 [ Java 2語言實務 ]

範例 Ex 2_2_2 01. public class Demo. Min. Variables{ 02. public static void main(String

範例 Ex 2_2_2 01. public class Demo. Min. Variables{ 02. public static void main(String args[]){ 03. // integers 04. byte smallest. Byte =Byte. MIN_VALUE; 05. //位元最小值 06. short smallest. Short = Short. MIN_VALUE; 07. //短整數最小值 08. int smallest. Integer = Integer. MIN_VALUE; 09. //整數最小值 10. long smallest. Long = Long. MIN_VALUE; 11. //長整數最小值 12. // real numbers 13. float smallest. Float = Float. MIN_VALUE; 14. //浮點數最小值 15. double smallest. Double= Double. MIN_VALUE; 16. //倍精數最小值 17. // other primitive types 18. boolean a. Boolean = false; //布林運算值 為假 26 19. // display them all 20. System. out. println("The smallest byte value " + smallest. Byte); 21. System. out. println("The smallest short value is " + smallest. Short); 22. System. out. println("The smallest integer value is " + smallest. Integer); 23. System. out. println("The smallest long value " + smallest. Long); 24. System. out. println("The smallest float value is " + smallest. Float); 25. System. out. println("The smallest double val is " + smallest. Double); 26. System. out. println("The value of a. Boolean i " + a. Boolean); 27. } 28. } [ Java 2語言實務 ]

執行結果: The The smallest smallest value of byte value is -128 short value is

執行結果: The The smallest smallest value of byte value is -128 short value is -32768 integer value is -2147483648 long value is -9223372036854775808 float value is 1. 4 E-45 double value is 4. 9 E-324 a. Boolean is false 27 [ Java 2語言實務 ]

列印控制字元 說明 Character string Backspace Unicode designation BS Backslash  \ Carriage Return CR

列印控制字元 說明 Character string Backspace Unicode designation BS Backslash \ Carriage Return CR r Double Quote " " Form Feed FF f Horizontal Tab HT t Single Quote ' ' Unicode Character 0 x#### u#### 29 b [ Java 2語言實務 ]

Ex 2_3_1 常數範例程式 class Demo. Constants { public static void main(String args[]) { final int

Ex 2_3_1 常數範例程式 class Demo. Constants { public static void main(String args[]) { final int workhour=44; final int workday; workday=5; System. out. println("In the future, labors must work " + workhour + " hours in " + workday + " days. "); // 在螢幕 上顯現出內容 } } 【執行結果】 In the future, labors must work 44 hours in 5 days. 2 -3、 常數 31 [ Java 2語言實務 ]

範例 Ex 2_3_2 計算 30 天 資 Ex 2_3_2 Demo. Constants 2. java 01. class

範例 Ex 2_3_2 計算 30 天 資 Ex 2_3_2 Demo. Constants 2. java 01. class Demo. Constants 2{ 02. public static void main(String args[]) { 03. final int daypayment=3000; 04. final int workday=30; 05. final int money=daypayment*workday; 06. System. out. println("I earn " + daypayment + " everyday, so I earn " + money + " every " + workday + "days. "); 07. // 在螢幕上顯現出內容 08. } 09. . } 執行結果: I earn 3000 everyday, so I earn 90000 every 30 days. 33 [ Java 2語言實務 ]

範例 Ex 2_3_2 說明 〔第 3 行〕final int daypayment=3000; 宣告一個常數其型態為 int (整數),將其命名為 daypayment,並有設定初始值定為 3000。 〔第

範例 Ex 2_3_2 說明 〔第 3 行〕final int daypayment=3000; 宣告一個常數其型態為 int (整數),將其命名為 daypayment,並有設定初始值定為 3000。 〔第 4 行〕final int workday=30; 宣告一個常數其型態也為 int,命名為 workday,其值設 定為 30。 〔第 5 行〕final int money=daypayment*workday; 定義 money 的其型態為 int,預設值為 daypayment 與 workday 的乘積。 〔第 6 行〕System. out. println("I earn " + daypayment + " everyday, so I earn " + money + " every + workday + "days). "); 將結果顯示出來。 34 [ Java 2語言實務 ]

Ex 2_4_1 運算子合併精簡使用範例 class Demo. Operators { public static void main(String args[]) { int a=20,

Ex 2_4_1 運算子合併精簡使用範例 class Demo. Operators { public static void main(String args[]) { int a=20, b=3, c=5; c *=5; b +=(2*c)+1; a %=((c-=18)-1); System. out. println("a=" + a + ", b=" + b + ", c=" + c ); } } 【執行結果】 a=2, b=54, c=7 2 -4、 運算式、運算子和運算元 39 [ Java 2語言實務 ]

Ex 2_4_2 Demo. Operators 2. java 01. class Demo. Operators 2 { 02. public

Ex 2_4_2 Demo. Operators 2. java 01. class Demo. Operators 2 { 02. public static void main(String args[]) { 03. int a=75, b=15, c=25; 04. System. out. println("a=" + a + ", b=" + b 05. c *=3; 06. System. out. println("c *=3"); 07. System. out. println("a=" + a + ", b=" + b 08. a %=b+5; 09. System. out. println("a %=b+5"); 10. System. out. println("a=" + a + ", b=" + b 11. b ^=((a &= 18) + (c |= 12)); 12. System. out. println("b ^=((a &= 18) + (c 13. System. out. println("a=" + a + ", b=" + b 14. } 15. } 41 + ", c=" + c ); |= 12))"); + ", c=" + c ); [ Java 2語言實務 ]

執行結果: a=75, b=15, c=25 c *=3 a=75, b=15, c=75 a %=b+5 a=15, b=15, c=75

執行結果: a=75, b=15, c=25 c *=3 a=75, b=15, c=75 a %=b+5 a=15, b=15, c=75 b ^=((a &= 18) + (c |= 12)) a=2, b=94, c=79 42 [ Java 2語言實務 ]

Ex 2_4_3 遞增、遞減運算子範例 class Demo. Increasement { public static void main(String args[]) { int a=7,

Ex 2_4_3 遞增、遞減運算子範例 class Demo. Increasement { public static void main(String args[]) { int a=7, b=3; int x=0, y=0, z=0; x=((--a)*3)+2; y=((b--)+8)*3; z=6*(a++)+8*(--b); System. out. println("a=" + a + ", b=" + b + ", x=" + x+ ", y=" + y+ ", z=" + z ); } } 【執行結果】 a=7,b=1,x=20,y=33,z=44 2 -4、 運算式、運算子和運算元 46 [ Java 2語言實務 ]

Ex 2_4_4 Demo. Increasement 2. java 01. class Demo. Increasement 2 { 02. public

Ex 2_4_4 Demo. Increasement 2. java 01. class Demo. Increasement 2 { 02. public static void main(String args[]){ 03. int a=12; 04. int x=0, y=0, z=0; 05. System. out. println("a=" + a + ", x=" + x+ ", y=" + y+ ", z=" + z); 06. x=((--a)*3)+2; 07. System. out. println("x=((--a)*3)+2"); 08. System. out. println("a=" + a + ", x=" + x+ ", y=" + y+ ", z=" + z); 09. y=((a--)+8)*3; 10. System. out. println("y=((a--)+8)*3"); 11. System. out. println("a=" + a + ", x=" + x+ ", y=" + y+ ", z=" + z); 12. z=6*(a++)+8*(--a); 13. System. out. println("z=6*(a++)+8*(--a)"); 14. System. out. println("a=" + a + ", x=" + x+ ", y=" + y+ ", z=" + z); 15. } 16. } 49 [ Java 2語言實務 ]

執行結果: a=12, x=0, y=0, z=0 x=((--a)*3)+2 a=11, x=35, y=0, z=0 y=((a--)+8)*3 a=10, x=35, y=57,

執行結果: a=12, x=0, y=0, z=0 x=((--a)*3)+2 a=11, x=35, y=0, z=0 y=((a--)+8)*3 a=10, x=35, y=57, z=0 z=6*(a++)+8*(--a) a=10, x=35, y=57, z=140 50 [ Java 2語言實務 ]

Ex 2_4_5 算數運算子範例 class Demo. Math { public static void main(String args[]) { int

Ex 2_4_5 算數運算子範例 class Demo. Math { public static void main(String args[]) { int x, y, z, q; int I=9; int mod; int J=3; x=I+J; //x=12 y=I-J; //y=6 z=I*J; //z=27 q=I/J; //q=3 mod=19%5; //mod=4 System. out. println("x=" + x + ", y=" + y + ", z=" + z + ", q=" + q+ ", mod=" + mod); } } 【執行結果】 x=12, y=6, z=27, q=3, mod=4 2 -4、 運算式、運算子和運算元 53 [ Java 2語言實務 ]

Ex 2_4_6 Demo. Math 2. java 01 class Demo. Math 2{ 02. public static

Ex 2_4_6 Demo. Math 2. java 01 class Demo. Math 2{ 02. public static void main(String args[]) { 03. int x=5; 04. int y=7; 05. int a=9; 06. int b=20; 07. int z=3; 08. int sum 1, sum 2, num 1, num 2, mod 1, mod 2; 09. System. out. println("香蕉有" + x + "根, 芭樂有" + y + "顆, " + "香蕉一根"+a + "元, 芭樂一顆" + b + "元, 小孩有" + z + "人"); 10. sum 1=x*a; 11. sum 2=y*b; 12. num 1=x/z; 13. num 2=y/z; 14. mod 1=x%z; 15. mod 2=y%z; 16. System. out. println("香蕉共計" + sum 1 + "元" + "n" + "芭樂共計"+ sum 2 +"元" + "n"+ "小孩一人可分得" + num 1 + "根香蕉以及"+ num 2 + "顆芭樂, 最後還剩" +mod 1 + "條香蕉以及"+ mod 2 + "顆芭樂"); 17. } 18. } 55 [ Java 2語言實務 ]

postfix operators [] 運 算 子 的 優 先 順 序 (params) exprt++ unary

postfix operators [] 運 算 子 的 優 先 順 序 (params) exprt++ unary operators ++expr --expr +expr creation or cast new multiplicative * / additive + shift << relational < equality == bitwise AND & expr--expr ~ ! (type) expr % >> > >>> <= >= instanceof != bitwise exclusive ^ OR bitwise inclusive | OR logical AND && logical OR || conditional ? : assignment = += -= *= /= >>>= 58 %= &= ^= |= <<= [ Java 2語言實務 ]

Ex 2_4_7 比較運算子範例 class Demo. Compare { public static void main(String[] args) { int

Ex 2_4_7 比較運算子範例 class Demo. Compare { public static void main(String[] args) { int x = 27; int y = 36; //宣告變數 System. out. println("Variable values as follows. . . "); System. out. println(" x = " + x); System. out. println(" y = " + y); System. out. println("Greater than. . . "); System. out. println(" x > y is " + (x > y)); //大於 (略) } } 【執行結果】 Variable values as follows. . . x = 27 y = 36 • (略) 2 -4、 運算式、運算子和運算元 60 [ Java 2語言實務 ]

Ex 2_4_9 邏輯運算子範例 class Demo. Logic { public static void main(String args[]) { boolean

Ex 2_4_9 邏輯運算子範例 class Demo. Logic { public static void main(String args[]) { boolean x, y; int i=10; x= (i >5) && (3>2); y=( i <5) && (3<2); System. out. println("x=" + x + ", y=" + y); } } 【執行結果】 x=true,y=false 2 -4、 運算式、運算子和運算元 67 [ Java 2語言實務 ]

Ex 2_4_10 class. Demo. Logic 2. java 01. class. Demo. Logic 2{ 02. public

Ex 2_4_10 class. Demo. Logic 2. java 01. class. Demo. Logic 2{ 02. public static void main(Stringargs[]){ 03. boolean x; 04. int i=10; 05. System. out. println("i 為正整數"+i); 06. x=(i>5)&&(3>2); 07. System. out. println("'i>5 且 3>2'這個描述為"+x); 08. x=(i<5)&&(3>2); 09. System. out. println("'i<5 且 3>2'這個描述為"+x); 10. x=(i<5)&&(3<2); 11. System. out. println("'i<5 且 3<2'這個描述為"+x); 12. x=(i>5)||(3>2); 13. System. out. println("'i>5 或 3>2'這個描述為"+x); 14. x=(i<5)||(3>2); 15. System. out. println("'i<5 或 3>2'這個描述為"+x); 16. x=(i<5)||(3<2); 17. System. out. println("'i<5 或 3<2'這個描述為"+x); 18. x=!(i>5); 19. System. out. println("'i>5 是錯的'這個描述為"+x); 20. x=!(i<5); 21. System. out. println("'i<5 是錯的'這個描述為"+x); 22. } 23. } 70 [ Java 2語言實務 ]

2_4_11 邏輯運算子複雜判斷範例 class Demo. Price{ public static void main(String args[]){ int age=53; int price=0;

2_4_11 邏輯運算子複雜判斷範例 class Demo. Price{ public static void main(String args[]){ int age=53; int price=0; if(age<18) price=50; if(age>=18 && age<60) price=100; if(age>=60) price=70; System. out. println("The price is "+ price + " dollars. "); } 【執行結果】 The price is 100 dollars. 2 -4、 運算式、運算子和運算元 73 [ Java 2語言實務 ]

Ex 2_4_12 Demo. Price 2. java 01. class Demo. Price 2{ 02. public static

Ex 2_4_12 Demo. Price 2. java 01. class Demo. Price 2{ 02. public static void main(String args[]){ 03. int score=85; //王同學本次考試平均成績 04. if(score<55) 05. System. out. println("王同學的考試成績為"+ 評比不及格" ); 06. if(score>=55 && score<=65) 07. System. out. println("王同學的考試成績為"+ 評比為及格邊緣" ); 08. System. out. println("王同學的考試成績為"+ 評比為尚可" ); 09. if(score>80) 10. System. out. println("王同學的考試成績為"+ 評比為高分" ); 11. } 12. } 75 score + "分, [ Java 2語言實務 ]

Ex 2_4_13 型態轉換範例 class Demo. Type. Converse { public static void main(String[] args) {

Ex 2_4_13 型態轉換範例 class Demo. Type. Converse { public static void main(String[] args) { int x=3; double y=2. 0; x=x+y; System. out. println(x); } } 【執行結果】 1 error (因為X為整數,而Y為雙精密浮點數) 2 -4、 運算式、運算子和運算元 80 [ Java 2語言實務 ]

Ex 2_4_14 強迫型態轉換範例 class Demo. Manul. Type. Converse { public static void main(String[] args)

Ex 2_4_14 強迫型態轉換範例 class Demo. Manul. Type. Converse { public static void main(String[] args) { double x=3. 0; int y=2; x=x+(double)y; System. out. println(x); } } 【執行結果】 5. 0 2 -4、 運算式、運算子和運算元 82 [ Java 2語言實務 ]

表 2 -14 型態轉換表 byte short int long char floot double byte int int

表 2 -14 型態轉換表 byte short int long char floot double byte int int long int floot double short int int long int floot double long long floot double char Int int long int floot double floot floot double double double 85 [ Java 2語言實務 ]