JAVA SDK 7 http www oracle comtechnetworkjavasedownloadsindex html

  • Slides: 41
Download presentation

開發環境 • JAVA SDK 7 http: //www. oracle. com/technetwork/javase/downloads/index. html • Net. Beans 或

開發環境 • JAVA SDK 7 http: //www. oracle. com/technetwork/javase/downloads/index. html • Net. Beans 或 Eclipse Net. Beans https: //netbeans. org/downloads/index. html Eclipse http: //www. eclipse. org/downloads/

第一隻程式 • public class Welcome { • public static void main(String agrs[]) { •

第一隻程式 • public class Welcome { • public static void main(String agrs[]) { • System. out. println("Hello JAVA"); • }

print差別 • System. out. print("Hello JAVA"); • System. out. printf("Hello JAVA"); • System. out.

print差別 • System. out. print("Hello JAVA"); • System. out. printf("Hello JAVA"); • System. out. println("Hello JAVA");

資料型態 Primitive Types / simple type 範圍 中文 byte (1 byte) -128 ~ 127

資料型態 Primitive Types / simple type 範圍 中文 byte (1 byte) -128 ~ 127 位元組 short (2 bytes) -32768 ~ 32767 短整數 int (4 bytes) -2147483648 ~ 2147483647 整數 long (8 bytes) -9223372036854775808 ~ 9223372036854775807 長整數 float (4 bytes) +-3. 4028237*10+38~ +1. 30239846*10 -45 浮點數 double (8 bytes) +1. 76769313486231570*10 +3 08~ 倍準數 4. 94065645841246544*10 324 char (2 bytes) Unicode characters (含中文) 字元 boolean (1 bytes) true或false 布林函數 String 字串 字串

輸入資料 • Scanner input = new Scanner(System. in); • int a = input. next.

輸入資料 • Scanner input = new Scanner(System. in); • int a = input. next. Int(); • System. out. println(a);

正確應該是 • int a = 10; • int b = 3; • float c

正確應該是 • int a = 10; • int b = 3; • float c = (float)a/b;

猜猜看會顯示什麼 • int a =123; • if(a==1) • System. out. print("1"); • System. out.

猜猜看會顯示什麼 • int a =123; • if(a==1) • System. out. print("1"); • System. out. println("2");

switch • JAVA 7 之後支援String 比較

switch • JAVA 7 之後支援String 比較

猜猜看 • int a= 10; • int b = 3; • System. out. println(a/b);

猜猜看 • int a= 10; • int b = 3; • System. out. println(a/b);

猜猜看 • int a= 10; • double b = 3; • System. out. println(a/b);

猜猜看 • int a= 10; • double b = 3; • System. out. println(a/b);

作業解說 • • Scanner input = new Scanner(System. in); System. out. println("請輸入直徑"); double r

作業解說 • • Scanner input = new Scanner(System. in); System. out. println("請輸入直徑"); double r = input. next. Double(); //取得半徑 double ans 1 = 2 * r; //直徑 double ans 2 = 2 * Math. PI * r; //周長 double ans 3 = Math. PI * r; //面積 System. out. printf("直徑=%f 周長=%f 面積=%fn", ans 1, ans 2, ans 3);

作業解說 2 • • • Scanner input = new Scanner(System. in); System. out. println("請輸入五位的數字");

作業解說 2 • • • Scanner input = new Scanner(System. in); System. out. println("請輸入五位的數字"); int n = input. next. Int(); if(n>=10000 && n<=99999) { for(int i=4; i>=0; i--) { System. out. printf("%d ", (int)(n/Math. pow(10, i))%10); } } else { System. out. println("你輸入的不是五位數字"); }

作業解說 2偷吃步 • Scanner input = new Scanner(System. in); • System. out. println("請輸入五位的數字"); •

作業解說 2偷吃步 • Scanner input = new Scanner(System. in); • System. out. println("請輸入五位的數字"); • String n = input. next. Line(); • if(n. length()==5) { • for(int i=1; i<=5; i++) { • System. out. printf("%s ", n. substring(i-1, i)); • } else { • System. out. println("你輸入的不是五位數字"); • }

Class類別(物件) • public class Cat { • String name = "kitty"; • String sound

Class類別(物件) • public class Cat { • String name = "kitty"; • String sound = "meow"; • int kg = 50; • }

副程式(動作、設定、取得) • public void get. Name() { • } • public String set. Name()

副程式(動作、設定、取得) • public void get. Name() { • } • public String set. Name() { • }

建構子(初始化) • public class Cat { • String name = "kitty"; • String sound

建構子(初始化) • public class Cat { • String name = "kitty"; • String sound = "meow"; • int kg = 50; • • public Cat(String name) { • this. name = name; • }

物件中的物件 • public class Cat { • String name = "kitty"; • String sound

物件中的物件 • public class Cat { • String name = "kitty"; • String sound = "meow"; • int kg = 50; • Eye lefteye = new Eye(); • Eye righteye = new Eye(); • }

陣列 • int x[]; 或 int []x; 或 int[] x; • x = new

陣列 • int x[]; 或 int []x; 或 int[] x; • x = new int[100]; • String s[]; 或 String[] s; 或 String []s; • s = new String[10];

物件陣列並不會自動初始化 • String s; • System. out. println(s); //NULL • String s[] = new

物件陣列並不會自動初始化 • String s; • System. out. println(s); //NULL • String s[] = new String [2]; • System. out. println(s[0]+s[1]); //ERROR

存取範圍 存取修飾 同一類別 同一套件 子類別 private default OK OK OK protected OK OK OK

存取範圍 存取修飾 同一類別 同一套件 子類別 private default OK OK OK protected OK OK OK public OK OK OK 全域 OK

Overloading(多載) • public void set. Name(String name) { • } • public void set.

Overloading(多載) • public void set. Name(String name) { • } • public void set. Name(String name 1, String name 2) { • }