1 / 28

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

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

Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. การเขียนโปรแกรม JAVA ขั้นพื้นฐาน โดย อ. นัฐพงศ์ ส่งเนียม http://www.siam2dev.com xnattapong@hotmail.com

  2. ตัวดำเนินการ(Operator) • ตัวดำเนินการทางคณิตศาสตร์(Arithmetic Operators) • ตัวดำเนินการแบบสัมพันธ์(Relational Operators) • ตัวดำเนินการระดับบิต(Bitwise Operators) • ตัวดำเนินการทางตรรกศาสตร์(Logical Operators)

  3. ตัวดำเนินการทางคณิตศาสตร์(Arithmetic Operators) • เครื่องหมาย ความหมาย ตัวอย่าง • + บวก a+b • - ลบ a-b • * คูณ a*b • / หาร a/b • % เศษจากการหาร a%b (Modulus)

  4. 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); } }

  5. ตัวดำเนินการแบบสัมพันธ์(Relational Operators) • เครื่องหมาย ความหมาย ตัวอย่าง • > มากกว่า a>b • >= มากกว่าหรือเท่ากับ a>=b • < น้อยกว่า a<b • <= น้อยกว่าหรือเท่ากับ a<=b • = = เท่ากับ a= =b • != ไม่เท่ากับ a!=b

  6. 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); • } • }

  7. ตัวดำเนินการระดับบิต(Bitwise Operators) • เครื่องหมาย ความหมาย ตัวอย่าง • >> Shift บิตไปทางขวา a>>b • << Shift บิตไปทางซ้าย a<<b • & and a&b • | or a|b • ^ xor a^b • ~ complement ~a

  8. 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)); } }

  9. ตัวดำเนินการทางตรรกศาสตร์(Logical Operators) • เครื่องหมาย ตัวอย่าง • && a && b (conditional) • || a || b (conditional) • ! !a

  10. if – else การตัดสินใจ • if(condition1) • statement1; • else if(condition2) • { • statement2; • statement3; • } • else statement4;

  11. Example Of if – else • Condition คือเงื่อนไขในการกระทำ • If => ถ้า • else => นอกเหนือจากif

  12. 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); • } • }

  13. 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."); • } • }

  14. 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."); • } • }

  15. 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."); • } • }

  16. Switch - case • switch(variable) • { case value1 : statement; statement; statement; break; case value2 : statement; statement; statement; break; default : }

  17. 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."); • } • } • }

  18. Array • Array คืออะไร • วิธีการประกาศ Array • ชนิดตัวแปร ชื่อตัวแปร[ ]; • ชนิดตัวแปร [ ]ชื่อตัวแปร; • วิธีการสร้าง Array • ตัวแปรที่ได้ประกาศว่าเป็นarray = new ชนิดตัวแปรนั้นๆ[n];

  19. 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]); } • }

  20. 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]); • } • }

  21. 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]); • } • }

  22. 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]); • } • }

  23. Array 2 Dimension ชนิดตัวแปร ชื่อตัวแปร[m][ ]; ชื่อตัวแปร[0] = ชนิดตัวแปร[n]; m = จำนวนแถว n= จำนวนคอลัมน์

  24. Example Of Array2 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]; • } • }

  25. Example Of Array2 Dimension(2)

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

  27. Assignment(2) • โจทย์ต้องการจะเก็บค่าในArrayที่มี 5 ช่อง โดยที่ช่องแรกถึงช่องที่ 4 นั้นเก็บค่าจากUser ส่วนช่องสุดท้ายให้เป็นผลรวมของช่องทั้งหมดที่ได้รับมาเช่น • Java assignment2 10 20 30 40 • Your summary is 100

  28. Assignment(3) • จงเขียนปิรามิดตามนี้ โดยใช้array 2 มิติ ปิรามิด : 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

More Related