28
กกกกกกกกกกกกกกก กกกกกกกกกกกกกกก JAVA JAVA กกกกกกกกกกก กกกกกกกกกกก กกก ก. กกกกกกก กกกกกกกก http://www.siam2dev.com [email protected]

การเขียนโปรแกรม JAVA ขั้นพื้นฐาน

Embed Size (px)

DESCRIPTION

การเขียนโปรแกรม JAVA ขั้นพื้นฐาน. โดย อ. นัฐพงศ์ ส่งเนียม http://www.siam2dev.com [email protected]. ตัวดำเนินการ( Operator ). ตัวดำเนินการทางคณิตศาสตร์( Arithmetic Operators) ตัวดำเนินการแบบสัมพันธ์( Relational Operators) ตัวดำเนินการระดับบิต( Bitwise Operators) - PowerPoint PPT Presentation

Citation preview

Page 1: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

การเขี�ยนโปรแกรมการเขี�ยนโปรแกรม JAVA JAVA ขี �นพื้��นขี �นพื้��นฐานฐาน

โดย อ. น ฐพื้งศ์� ส่�งเน�ยมhttp://www.siam2dev.com

[email protected]

Page 2: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

ตั�วดำ��เนินิก�ร(Operator)

ตั วด�าเน�นการทางคณิ�ตัศ์าส่ตัร�(Arithmetic Operators)

ตั วด�าเน�นการแบบส่ มพื้ นธ์�(Relational Operators)

ตั วด�าเน�นการระด บบ�ตั(Bitwise Operators)ตั วด�าเน�นการทางตัรรกศ์าส่ตัร�(Logical

Operators)

Page 3: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

ตั�วดำ��เนินิก�รท�งคณิตัศ�สตัร�(Arithmetic Operators)

เคร��องหม�ย คว�มหม�ย ตั�วอย��ง+ บวก a+b- ลบ a-b* ค%ณิ a*b/ หาร a/b% เศ์ษจากการหาร a%b (Modulus)

Page 4: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Arithmetic Operators

public class testing //ไฟล�ชื่�,อtesting.java{

public static void main(){ int a=5,b=2,c=6; a=a+b; b=a-c; b=b*2; a=a/2; c=a%b;

System.out.println("a="+a+",b="+b+",c="+c);}

}

Page 5: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

ตั�วดำ��เนินิก�รแบบส�มพั�นิธ์�(Relational Operators)

เคร��องหม�ย คว�มหม�ย ตั�วอย��ง> มากกว�า a>b>= มากกว�าหร�อเท�าก บ a>=b< น-อยกว�า a<b<= น-อยกว�าหร�อเท�าก บ a<=b= = เท�าก บ a= =b!= ไม�เท�าก บ a!=b

Page 6: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Relational Operators

public class testing { public static void main(String args[]) { int value1=10,value2=20,value3=10; System.out.println(value1>value2); System.out.println(value1>=value3); System.out.println(value1<value2); System.out.println(value1<=value3); System.out.println(value1==value2); System.out.println(value1!=value2); } }

Page 7: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

ตั�วดำ��เนินิก�รระดำ�บบตั(Bitwise Operators)เคร��องหม�ย คว�มหม�ย

ตั�วอย��ง>> Shift บ�ตัไปทางขีวา a>>b<< Shift บ�ตัไปทางซ้-าย a<<b& and a&b| or a|b^ xor a^b~ complement ~a

Page 8: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Bitwise Operators

public class test // ไฟล�ชื่�,อtest.java{

public static void main(String args[]) { System.out.println(“7>>2=”+(7>>2));

System.out.println(“7<<2=”+(7<<2)); System.out.println(“5&1 =”+(5&1)); System.out.println(“5|2 =”+(5|2)); System.out.println(“1^6 =”+(1^6)); System.out.println(“~7 =”+(~7));}

}

Page 9: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

ตั�วดำ��เนินิก�รท�งตัรรกศ�สตัร�(Logical Operators)

เคร��องหม�ย ตั�วอย��ง&& a && b (conditional)|| a || b (conditional)! !a

Page 10: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

if – else ก�รตั�ดำสนิใจ if(condition1) statement1; else if(condition2) { statement2; statement3; } else

statement4;

Page 11: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of if – else

Condition ค�อเง�,อนไขีในการกระท�าIf => ถ้-าelse => นอกเหน�อจากif

Page 12: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of if – else (2)

public class testing { public static void main(String args[]) { boolean a=true; char A='T',B='F'; if(a) System.out.println(A); else System.out.println(B); } }

Page 13: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of if – else (3)

public class testing { public static void main(String args[]) { int a=Integer.parseInt(args[0]); if(a>50) System.out.println("The Value is higher than fifty."); else if(a==50) System.out.println("The Value is equal fifty."); else System.out.println("The Value is lower than fifty."); } }

Page 14: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of if – else (3)

public class testing { public static void main(String args[]) { int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); if(a>50&&b>50) System.out.println("The Value A And B are higher than fifty."); else if(a==50&&b==50) System.out.println("The Value A And B are equal fifty."); else if(a<50&&b<50) System.out.println("The Value A And B are lower than fifty."); else if(a>50||b>50) System.out.println("The Value A Or B is higher than fifty."); else if(a==50||b==50) System.out.println("The Value A Or B are equal fifty."); else if(a<50||b<50) System.out.println("The Value A Or B are lower than fifty."); } }

Page 15: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of if – else (4)

public class testing { public static void main(String args[]) { int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); if(a>50&&b>50) System.out.println("The Value A And B are higher than fifty."); if(a==50&&b==50) System.out.println("The Value A And B are equal fifty."); if(a<50&&b<50) System.out.println("The Value A And B are lower than fifty."); if(a>50||b>50) System.out.println("The Value A Or B is higher than fifty."); if(a==50||b==50) System.out.println("The Value A Or B are equal fifty."); if(a<50||b<50) System.out.println("The Value A Or B are lower than fifty."); } }

Page 16: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Switch - case

switch(variable) {

case value1 : statement; statement; statement; break;

case value2 : statement; statement; statement; break;

default :

}

Page 17: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Switch - case

public class testing { public static void main(String args[]) { int a=Integer.parseInt(args[0]); switch(a) { case (50):System.out.println("The Value is equal fifty."); case (40):System.out.println("The Value is equal forty."); case (30):System.out.println("The Value is equal thirty."); default : System.out.println(“Not equal Anything."); } } }

Page 18: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Array

Array ค�ออะไรว�ธ์�การประกาศ์ Array

ชื่น�ดตั วแปร ชื่�,อตั วแปร[ ]; ชื่น�ดตั วแปร [ ]ชื่�,อตั วแปร;

ว�ธ์�การส่ร-าง Array ตั วแปรท�,ได-ประกาศ์ว�าเป1นarray = new ชื่น�ดตั วแป

รน �นๆ[n];

Page 19: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Array public class testing { public static void main(String args[]) { int []a; int b[]; a = new int[3]; b = new int[2];

int c[] = new int[2]; a[0]=1; a[1]=2; a[2]=3; b[0]=4; b[1]=5; c[0]=6; c[1]=7; System.out.print(a[0]+""+a[1]+""+a[2]); System.out.print(b[0]+""+b[1]+""+c[0]+""+c[1]);

} }

Page 20: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Array (2)

public class testing { public static void main(String args[]) { int []a,b,c; a = new int[3]; b = new int[2]; c = new int[2]; a[0]=1; a[1]=2; a[2]=3; b[0]=4; b[1]=5; c[0]=6; c[1]=7; System.out.print(a[0]+""+a[1]+""+a[2]); System.out.print(b[0]+""+b[1]+""+c[0]+""+c[1]); } }

Page 21: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Array (3)

public class testing { public static void main(String args[]) {

String arr[] = {“Testing” , “Test” , “end of array”}; int a[] = {1,2,3}; int b[] = {4,5}; int c[] = {6,7}; System.out.print(a[0]+""+a[1]+""+a[2]); System.out.print(b[0]+""+b[1]+""+c[0]+""+c[1]);

System.out.print(arr[0]+arr[1]+arr[2]); } }

Page 22: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Array (4)

public class testing { public static void main(String args[]) { String art[] = {"Testing", "Test", "end of array" }; int a[] = {1,2,3}; int b[] = {4,5}; int c[] = {6,7}; System.out.print(a[0]+""+a[1]+""+a[2]); System.out.print(b[0]+""+b[1]+""+c[0]+""+c[1]+"\n"); System.out.print(art[0]+" "+art[1]+" "+art[2]); } }

Page 23: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Array 2 Dimension

ชื่น�ดตั วแปร ชื่�,อตั วแปร[m][ ];

ชื่�,อตั วแปร[0] = ชื่น�ดตั วแปร[n];

m = จ�านวนแถ้วn = จ�านวนคอล มน�

Page 24: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Array 2 Dimension

public class testing { public static void main(String args[]) { int a[][] = new int[3][]; a[0] = new int[1]; a[1] = new int[2]; a[2] = new int[3]; } }

Page 25: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Example Of Array 2 Dimension(2)

Page 26: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Assignment

ท�าการร บค�าคะแนนเก3บขีองน�ส่�ตัหน4,งคน แล-วหาว�าน�ส่�ตัคนน �นได-เกรดอะไร โดยม�เกณิฑ์�การค�ดเกรดตัามน�� A >80 B+ >75<=80 B >70<=75 C+ >65<=70 C >60<=65 D+ >55<=60 D >50<=55 F <=50เชื่�นJava testing 90Your Grade is A!!!.

Page 27: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Assignment (2)

โจทย�ตั-องการจะเก3บค�าในArrayท�,ม� 5 ชื่�อง โดยท�,ชื่�องแรกถ้4งชื่�องท�, 4 น �นเก3บค�าจากUser ส่�วนชื่�องส่6ดท-ายให-เป1นผลรวมขีองชื่�องท �งหมดท�,ได-ร บมาเชื่�น

Java assignment2 10 20 30 40Your summary is 100

Page 28: การเขียนโปรแกรม  JAVA  ขั้นพื้นฐาน

Assignment (3)

จงเขี�ยนป8ราม�ดตัามน�� โดยใชื่-array 2 ม�ตั�ป8ราม�ด :1

2 3

5 8 13

21 34 55 89

144 233 377 610 987