การเขียนโปรแกรม java ขั้นพื้นฐาน
DESCRIPTION
การเขียนโปรแกรม JAVA ขั้นพื้นฐาน. โดย อ. นัฐพงศ์ ส่งเนียม http://www.siam2dev.com [email protected]. ตัวดำเนินการ( Operator ). ตัวดำเนินการทางคณิตศาสตร์( Arithmetic Operators) ตัวดำเนินการแบบสัมพันธ์( Relational Operators) ตัวดำเนินการระดับบิต( Bitwise Operators) - PowerPoint PPT PresentationTRANSCRIPT
การเขี�ยนโปรแกรมการเขี�ยนโปรแกรม JAVA JAVA ขี �นพื้��นขี �นพื้��นฐานฐาน
โดย อ. น ฐพื้งศ์� ส่�งเน�ยมhttp://www.siam2dev.com
ตั�วดำ��เนินิก�ร(Operator)
ตั วด�าเน�นการทางคณิ�ตัศ์าส่ตัร�(Arithmetic Operators)
ตั วด�าเน�นการแบบส่ มพื้ นธ์�(Relational Operators)
ตั วด�าเน�นการระด บบ�ตั(Bitwise Operators)ตั วด�าเน�นการทางตัรรกศ์าส่ตัร�(Logical
Operators)
ตั�วดำ��เนินิก�รท�งคณิตัศ�สตัร�(Arithmetic Operators)
เคร��องหม�ย คว�มหม�ย ตั�วอย��ง+ บวก a+b- ลบ a-b* ค%ณิ a*b/ หาร a/b% เศ์ษจากการหาร a%b (Modulus)
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);}
}
ตั�วดำ��เนินิก�รแบบส�มพั�นิธ์�(Relational Operators)
เคร��องหม�ย คว�มหม�ย ตั�วอย��ง> มากกว�า a>b>= มากกว�าหร�อเท�าก บ a>=b< น-อยกว�า a<b<= น-อยกว�าหร�อเท�าก บ a<=b= = เท�าก บ a= =b!= ไม�เท�าก บ a!=b
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); } }
ตั�วดำ��เนินิก�รระดำ�บบตั(Bitwise Operators)เคร��องหม�ย คว�มหม�ย
ตั�วอย��ง>> Shift บ�ตัไปทางขีวา a>>b<< Shift บ�ตัไปทางซ้-าย a<<b& and a&b| or a|b^ xor a^b~ complement ~a
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));}
}
ตั�วดำ��เนินิก�รท�งตัรรกศ�สตัร�(Logical Operators)
เคร��องหม�ย ตั�วอย��ง&& a && b (conditional)|| a || b (conditional)! !a
if – else ก�รตั�ดำสนิใจ if(condition1) statement1; else if(condition2) { statement2; statement3; } else
statement4;
Example Of if – else
Condition ค�อเง�,อนไขีในการกระท�าIf => ถ้-าelse => นอกเหน�อจากif
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); } }
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."); } }
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."); } }
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."); } }
Switch - case
switch(variable) {
case value1 : statement; statement; statement; break;
case value2 : statement; statement; statement; break;
default :
}
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."); } } }
Array
Array ค�ออะไรว�ธ์�การประกาศ์ Array
ชื่น�ดตั วแปร ชื่�,อตั วแปร[ ]; ชื่น�ดตั วแปร [ ]ชื่�,อตั วแปร;
ว�ธ์�การส่ร-าง Array ตั วแปรท�,ได-ประกาศ์ว�าเป1นarray = new ชื่น�ดตั วแป
รน �นๆ[n];
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]);
} }
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]); } }
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]); } }
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]); } }
Array 2 Dimension
ชื่น�ดตั วแปร ชื่�,อตั วแปร[m][ ];
ชื่�,อตั วแปร[0] = ชื่น�ดตั วแปร[n];
m = จ�านวนแถ้วn = จ�านวนคอล มน�
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]; } }
Example Of Array 2 Dimension(2)
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!!!.
Assignment (2)
โจทย�ตั-องการจะเก3บค�าในArrayท�,ม� 5 ชื่�อง โดยท�,ชื่�องแรกถ้4งชื่�องท�, 4 น �นเก3บค�าจากUser ส่�วนชื่�องส่6ดท-ายให-เป1นผลรวมขีองชื่�องท �งหมดท�,ได-ร บมาเชื่�น
Java assignment2 10 20 30 40Your summary is 100
Assignment (3)
จงเขี�ยนป8ราม�ดตัามน�� โดยใชื่-array 2 ม�ตั�ป8ราม�ด :1
2 3
5 8 13
21 34 55 89
144 233 377 610 987