หน้าเว็บ

วันอาทิตย์ที่ 9 ธันวาคม พ.ศ. 2555

การใช้ switch..case


switch..case นั้นจะใช้กับการตัดสินใจที่มีขนาดใหญ่ขึ้นหรือมีเงื่อนไขหลายระดับชั้นมาก
ขึ้น แต่จะทำให้คุณนั้นเขียน Code ได้กระชับและสั้นลง มักนิยมนำ  switch..case มาใช้ในการสร้าง Menu
Syntax :




ตัวอย่างโปรแกรม : การใช้ตัวแปรชนิด int กับ switch..case
Output:


Source Code:
1 //Pswitch.java
2 public class Pswitch {
3 public static void main (String agrs[])
4 {
5 int a = 1;
6 System.out.println ("==================");
7 System.out.println (" Main Menu ");
8 System.out.println ("=================");
9 System.out.println ("1. Java ");
10 System.out.println ("2. Visual C++ ");
11 System.out.println ("3. C# ");
12 System.out.println ("=================");
13 switch (a)
14 {
15 case 1 : System.out.println ("Choose Java");
16 break;
17 case 2 : System.out.println ("Choose Visual C++");
18 break;
19 case 3 : System.out.println ("Choose C#");
20 break;
21 default : System.out.println ("Out of Range");
22 } //end switch..case
23 } //end main()
24 }//end class


  



กลุ่มคำสั่งในการทำซ้า (Repetition)


กลุ่มคำ สั่งในการทำซ้า (Repetition) หรือ กลุ่มคำสั่งในการวนรอบการทำงาน (Loop) ซึ่งกลุ่มคำสั่งเหล่านี้จะทำซ้ำ(วน Loop) เมื่อเงื่อนไขเป็นจริง (True) สามารถจำแนกการใช้ Repetition ได้   3   ประเภท ดังนี้
1. การใช้   for
       ลูป for ใช้เมื่อทราบจำนวนครั้งของการทำซ้ำ ที่แน่นอน ซึ่งลูป for จัดเป็นลูปที่ใช้งานง่าย
และพบบ่อยที่สุด รูปแบบของ for ประกอบด้วย 3 นิพจน์ ดัง Syntax ด้านล่างนี้
Syntax:

ตัวอย่างโปรแกรม : การทำงานวนรอบด้วยลูป for
Output:




Source Code:
1 //Pfor.java
2 public class Pfor {
3 public static void main (String agrs[])
4 {
5 int a,r=0;
6 for (a=0;a<10;a++)
7 {
8 System.out.println ("Value of round # "+r+" = "+a);
9 a++;
10 r++;
11 } //end loop
12 } // end main()
13 } //end class

ตัวอย่างโปรแกรม : สร้างสูตรคูณแม่ 2-12 โดยใช้ลูป for
Output:




Source Code:
1 //Multiply.java
2 public class Multiply{
3 public static void main(String agrs[])
4 {
5 int c;
6 for (int a= 2;a<=12;a++ ) //แม่สูตรคูณ 2-12
7 { for (int b=1;b<=12 ;b++ ) // ลา ดับการคูณ 1-12
8 { c= a*b;
9 System.out.print(" "+c+" ");
10 } //end inside for
11 System.out.print(" \n ");
12 } //end outside for
13 }//end main()
14 } //end class

ตัวอย่างโปรแกรม :
Output:




Source Code:
1 // ForDL.java
2 import java.awt.Graphics;
3 import javax.swing.JApplet;
4
5 public class ForDL extends JApplet {
6 public void paint( Graphics g ) // draw lines on applet’s background
7 {
8 super.paint( g ); // call inherited version of method paint
9 for ( int line = 1; line <= 15; line++ ) // 15 line
10 g.drawLine( 10, 10, 250, line * 10 );
11
12 } // end method paint
13 } // end class
               
           เมื่อคุณสร้าง File (.java) และ Compile ผ่านเรียบร้อยแล้ว และโปรแกรมนี้เป็น Java Applet ดังนั้นจงอย่าลืมว่าคุณยังต้องไปสร้าง File เอกสาร HTML แล้วฝัง File (.class) ที่ได้ลงไปจากนั้น Run ดู  ผลลัพธ์โดยใช้ IE Browser








การใช้ do..while


 การใช้ do..while นั้น จะใช้เมื่อเราต้องการเข้าสู่การทา งานของลูป (Loop) อย่างน้อย 1
ครั้ง โดยจะวางนิพจน์ทดสอบค่าไว้ตอนท้ายของ Loop
Syntax :




while (condition);    อย่าลืมใส่เครื่องหมาย ( ; ) หลังเงื่อนไข while ไม่เช่นนั้นโปรแกรมจะค้าง (hang)

ตัวอย่างโปรแกรม : ให้ทา งานในลูป do..while 6 รอบแล้วแสดงค่าตัวเลขตัวสุดท้าย

Output:

Source Code:
1 //Pdowhile.java
2 public class Pdowhile{
3 public static void main (String agrs[])
4 {
5 int a=0,b=0,count=0;
6
7 do { //เข้าสู่ loop อย่างน้อย 1 ครั้ง
8 a += 1;
9 b += a;
10 count++;
11 System.out.println ("value b in loop = "+b);
12 }
13 while (a <= 5); // ตรวจสอบเงื่อนไข เป็นเท็จ จบ loop
14 System.out.println ("=======================");
15 System.out.println ("The round of loop = "+count);
16 System.out.println ("last value b = "+b);
17 } //end main
18 } //end class


ตัวอย่างโปรแกรม :รับค่าอายุ หรือ ปี ค.ศ. ที่เกิด แล้วคา นวณหาอายุ หรือคา นวณว่าเกิดปี ค.ศ. ใดหลังจากนั้น ถาม User ว่าต้องการทา งานอีกรอบหรือไม่ ถ้าต้องการทา กดเลข 1

Output:



Source Code:
1 // Ans2.java
2 import javax.swing.JOptionPane;
3
4 public class Ans2 {
5 public static void main( String args[] )
6 {
7 int mul=1,
8 check=0,a;
9 String i,b;
10 do
11 {
12 i = JOptionPane.showInputDialog(
13 "Please Enter Year Age : " );
14 check = Integer.parseInt(i);
15 mul = 2006- check;
16
17 JOptionPane.showMessageDialog( null,
18 "Result (Age/Birth Year) = " + mul, " ",
19 JOptionPane.INFORMATION_MESSAGE );
20 // System.exit( 0 );
21
22 b = JOptionPane.showInputDialog(
23 "Do you want to Continue. If say yes Press 1 : " );
24 a = Integer.parseInt(b);
25 }
26 while ( a == 1); //end do..while
27 System.exit( 0 );
28 } //end main()
29 } // end class Ans2

ตัวอย่างโปรแกรม : การใช้ do..while สร้างรูปวงกลมใน Java Applet
Output:

Source Code:
1 // DWhile.java
2 import java.awt.Graphics;
3
4 import javax.swing.JApplet;
5 public class DWhile extends JApplet {
6 public void paint( Graphics g ) // draw lines on applet’s background
7 {
8
9 super.paint( g ); // call inherited version of method paint
10 int ov = 1;
11 do {
12 g.drawOval( 320 - ov * 40, 320 - ov * 40,
13 ov * 40, ov * 40 );
14 ++ov;
15 } while ( ov <= 5 ); // end do...while
16
17 ov=1;
18 do {
19 g.drawOval( 110 - ov * 10, 110 - ov * 10,
20 ov * 20, ov * 20 );
21 ++ov;
22 } while ( ov <= 10 ); // end do...while
23 } // end method paint
24 } // end class DoWhileTest

Run โดยใช้ IE Browser





การใช้ while


ลูป while จะทำการตรวจสอบเงื่อนไขก่อน หากพบว่าเงื่อนไขเป็นเท็จจะไม่เข้าไปทำ Statement ภายในลูป
Syntax :



ตัวอย่างโปรแกรม : การกา หนดค่าคงที่กับลูป while
Output:
9
10
11
12
13
14
Finish loop and number of count 6

Source Code:
1 //Pwhile.java
2 public class Pwhile {
3 public static void main (String agrs[])
4 {
5 int i = 9 ;
6 int count = 0;
7 while (i != 15) // เงื่อนไขเป็นจริง จะเข้าสู่ loop
8 { System.out.println (i);
9 i++;
10 count++;
11 } //end while
12 System.out.println ("Finish loop and number of count "+count);
13 System.exit(0);
14 } //end main()
15 } //end class

ตัวอย่างโปรแกรม : การใช้ลูป while รับค่าคะแนนโดยใช้ JOptionPane แล้วคำนวณหาค่าเฉลี่ย
Output:

Source Code:
1 // Average1.java
2 import javax.swing.JOptionPane;
3
4 public class Average1 {
5 public static void main( String args[] )
6 {
7 int total, // sum of grades input by user
8 gradeCounter, // number of grades entered
9 gradeValue, // grade value
10 average; // average of all grades
11 String grade; // grade typed by user
12
13 // Initialization Phase
14 total = 0; // clear total
15 gradeCounter = 1; // prepare to loop
16
17 // Processing Phase
18 while ( gradeCounter <= 5 ) { // loop 5 times
19
20 // prompt for input and read grade from user
21 grade = JOptionPane.showInputDialog(
22 "Enter integer mark of grade: " );
23 gradeValue = Integer.parseInt( grade ); // convert grade from a String to an integer
24 total = total + gradeValue; // add gradeValue to total
25 gradeCounter = gradeCounter + 1; // add 1 to gradeCounter
26 } // end while structure
27 average = total / 10; // perform integer division
28
29 // display average of exam grades
30 JOptionPane.showMessageDialog( null,
31 " Average = " + average, "Average Class ",
32 JOptionPane.INFORMATION_MESSAGE );
33
34 System.exit( 0 ); // terminate the program
35 } // end method main
36 } // end class Average1