SlideShare a Scribd company logo
1 of 27
1
คำสั่ง if
• simple if
รูปแบบ if (เงื่อนไข)
{ คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นจริง; }
ตัวอย่ำง
if ( radius >= 0 )
{ area = radius * radius * PI;
System.out.println (“พื้นที่วงกลม คือ ” + area); }
if (( i >= 0 ) and ( i <= 10 ))
{ System.out.println ( “i เป็นตัวเลขระหว่ำง 0 ถึง 10” ); }
if ( sex == ‘m’ || sex == ‘M’ )
return height-100;
2
• if..else
รูปแบบ
if (เงื่อนไข)
{ คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นจริง; }
else
{ คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นเท็จ; }
ตัวอย่ำง
if ( radius >= 0 )
{ area = radius * radius * PI;
System.out.println (“พื้นที่วงกลม คือ ” + area);
}
else
{ System.out.println (“ตัวเลขเป็นค่ำลบไม่ได ้”); }
if ( sex == ‘m’ || sex == ‘M’ )
{ return height-100; }
else
{ return height-110; }
3
4
• Nested if
แบบที่ 1
if (เงื่อนไข1)
{ if (เงื่อนไข2)
{ if (เงื่อนไข3)

}
}
else
{ คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นเท็จ; }
แบบที่ 2
if (เงื่อนไข1) { คำสั่งเมื่อเงื่อนไข1 เป็นจริง; }
else
if (เงื่อนไข2) { คำสั่งเมื่อเงื่อนไข2 เป็นจริง; }
else
if (เงื่อนไข3) { คำสั่งเมื่อเงื่อนไข3 เป็นจริง; }

5
ตัวอย่ำง
if ( i > k ) {
if ( j > k )
System.out.println (“i และ j มีค่ำมำกกว่ำ k”); }
else
{ System.out.println (“i มีค่ำน้อยกว่ำหรือเท่ำกับ k”); }
if ( score >= 90.0 )
{ grade = ‘A’; }
else
if (score >= 80.0 )
{ grade = ‘B’; }
else
if ( score >= 70.0 )
{ grade = ‘C’;}
else
{ grade = ‘F’; }
6
import java.io.*;
class TestIf
{ public static void main(String[] args) throws IOException
{ BufferedReader Datain =new BufferedReader
(new InputStreamReader (System.in));
System.out.print("Please key your score => ");
String input = Datain.readLine();
int score = Integer.parseInt(input);
System.out.println("Your input data is => " + score);
char grade;
if (score >= 90.0 )
{ grade = 'A'; }
else
if (score >= 80.0 )
{ grade = 'B'; }
else
if (score >= 70.0 )
{ grade = 'C'; }
else
{ grade = 'F'; }
System.out.println("Your grade is => " + grade); } }
7
8
• Shortcut if
รูปแบบ
ตัวแปร = (เงื่อนไข)? คำสั่งเมื่อเงื่อนไขเป็นจริง : คำสั่งเมื่อเงื่อนไขเป็นเ็็จ
ตัวอย่ำง if ( x > 0 ) y = 1
else y = -1; หรือ
y = ( x > 0 ) ? 1 : -1
class Ternary
{ public static void main(String[] args)
{ int a,b;
a = 10;
b = a < 10 ? -a : a;
System.out.println("Absolute value of a is " + b);
a = -10;
b = a < 10 ? -a : a;
System.out.println("Absolute value of a is " + b); } }
9
1. จงแสดงผลลัพธ์จำกกำรทำงำนของโปรแกรมต่อไปนี้ (item1.java)
public class three
{ public static void main(String args[])
{ int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6;
int ans1 = c * c + c % b;
int ans2 = b + e / c - c * d;
int ans3 = b * (a - (d / e) / b) * (b - e % c);
int ans4 = a + b - c / d / e * f;
System.out.println("ans1 is " + ans1 + " และ " + "ans2 is " + ans2);
System.out.println("ans3 is " + ans3 + " และ " + "ans4 is " + ans4);
}
}
แบบฝึกหัด ครั้งที่ 1
10
2. จงแสดงผลลัพธ์จำกกำรทำงำนของโปรแกรมต่อไปนี้
(item2.java)
public class four
{ public static void main(String args[])
{ boolean a = true, b = true, c = true;
boolean ans1 = !a && b;
boolean ans2 = a && b || c;
boolean ans3 = a || (b && c);
boolean ans4 = a && b || c;
System.out.print("ans1 is " + ans1 + " และ ");
System.out.println("ans2 is " + ans2);
System.out.print("ans3 is " + ans3 + " และ ");
System.out.println("ans4 is " + ans4); } }
11
3. จงเขียนโปรแกรมเพื่อคำนวณผลลัพธ์ของนิพจน์ต่อไปนี้
5 + 1 / 7
3 * 3 + 3 % 2
2 + 5 / 3 + -3 * 4
2 * (1 + -(4 / 5) / 2) * (2 - 5 % 3)
4. จงเขียนโปรแกรมเพื่อตรวจสอบผลกำรคำนวณของนิพจน์ต่อไปนี้
a+b*c!=b/c%2&&a*b/--c||b++/a>0
a*b/c==b>0||a%b+--a/c++
12
คำสั่ง Switch
รูปแบบ
switch (ตัวแปร)
{ case ค่ำที่ 1 : คำสั่งที่ 1;
break;
case ค่ำที่ 2 : คำสั่งที่ 2;
break;

case ค่ำที่ N : คำสั่งที่ N;
break;
default : คำสั่งเมื่อไม่มีค่ำที่ตรงกับค่ำที่ระบุใน case;
}
13
ตัวอย่ำง
switch (year)
{ case 5 : IRATE = 12;
break;
case 15 : IRATE = 18;
break;
case 30 : IRATE = 24;
break;
default : System.out.println(“ตัวเลขผิด โปรดแก ้ไข”);}
14
15
คำสั่ง While
รูปแบบ
while (เงื่อนไข)
{ คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นจริง; }
ตัวอย่ำง
while (Number >= 1.0)
{ Number *= 0.1;
TimesMoved++; }
16
17
คำสั่ง Do..While
รูปแบบ
do
{ คำสั่งต่ำง ๆ; }
while (เงื่อนไข);
ตัวอย่ำง
do
{ data = data + 1;
sum += data; }
while (data <= 10);
18
19
คำสั่ง For
รูปแบบ
for (ค่ำตัวแปรเริ่มต ้น ; เงื่อนไข ; เปลี่ยนแปลงค่ำตัวแปร)
ตัวอย่ำง
int i;
for ( i = 0 ; i < 100 ; i++ )
{ System.out.println (“ Warm Welcome”); }
for ( i = 0; i < 10; i++ )
{ for ( j = i; j < 10; j++ )
System.out.print( " * " );
System.out.println( ); }
20
import java.io.*;
public class Reverse
{ public static void main(String[] args) throws IOException
{ BufferedReader stdin = new BufferedReader
(new InputStreamReader (System.in));
System.out.println("Please key your text ");
String s = stdin.readLine( );
String r = " ";
for (int i = 0; i < s.length( ); i++)
{ char ch = s.charAt(i);
r = ch + r;
System.out.println (r); }
System.out.println (s + " Reverse of your text is " + r); } }
21
คำสั่ง Break และ Continue
• Break หำกโปรแกรมพบคำสั่งนี้จะหลุดออกจำก Loop กำร
ทำงำนทันที
• Continue หำกโปรแกรมพบคำสั่งนี้จะหยุดกำรทำงำนที่จุด
นั้น แล ้วย ้อนกลับไปเริ่มต ้นกำรทำงำนที่ Loop ใหม่
ตัวอย่ำง
while (1 < 2)
{ Number = Number + 1;
if ( Number == 10 )
{ System.out.println (" จบกำรทำงำน");
break; }
if ( Number > 0 && Number <= 9)
{ System.out.println (" ตัวเลข " + Number );
continue; } }
22
outer : for (int i = 0; i < 3; i++ )
{ System.out.print (" Pass " + i + " : " );
for (int j = 0; j < 10; j++ )
{ System.out.print ( j+ " " ); }
break outer; }
outer : for (int i = 0; i < 10; i++ )
{ for (int j = 0; j < 10; j++ )
{ if (j > i)
{ System.out.println ( );
continue outer; }
System.out.print (" " + ( i*j )); } }
23
public class breaklabel
{ public static void main(String[] args)
{ outer : for (int i = 0; i < 3; i++ )
{ System.out.print (" Pass " + i + " : " );
for (int j = 0; j < 10; j++ )
{ System.out.print ( j+ " " ); }
break outer; }
System.out.println (" Loops complete. " ); } }
24
public class continuelabel
{ public static void main(String[] args)
{ outer : for (int i = 0; i < 10; i++ )
{ for (int j = 0; j < 10; j++ )
{ if (j > i)
{ System.out.println ( );
continue outer; }
System.out.print (" " + ( i*j )); } }
System.out.println ( ); } }
25
public class Table1
{ public static void main (String[] args)
{ final int COLUMN_WIDTH = 8;
for (int x = 1; x <= 5; x++)
{ for (int y = 1; y <= 4; y++)
{ int p = (int)Math.pow(x,y);
String pstr = " " + p;
while (pstr.length() < COLUMN_WIDTH)
pstr = " " + pstr;
System.out.print(pstr); }
System.out.println(); } } }
26
แบบฝึกหัดที่ 1
• จงเขียนโปรแกรมเพื่อรับข ้อมูลชื่อและนำมสกุล และวันเดือนปีเกิดของผู้ใช ้
จำกหน้ำจอภำพ แล ้วสร ้ำงรหัสชุดหนึ่งประกอบด ้วยตัวอักษร 5 ตัว ตัวแรก
ได ้มำจำกอักษรแรกของชื่อผู้ใช ้ตัวที่สองได ้มำจำกควำมยำวของชื่อผู้ใช ้
ตัวที่สำมและสี่ได ้มำจำกรหัส ASC II ของอักษรแรกของนำมสกุลผู้ใช ้และ
ตัวที่ห ้ำได ้มำจำกอักษรตัวสุดท ้ำยของนำมสกุลผู้ใช ้จำกนั้นให ้แสดง
แสดงผลรหัสชุดดังกล่ำวและอำยุจำนวนกี่ปีและกี่เดือนของผู้ใช ้ณ วันที่ 12
ธ.ค. 55 หน้ำจอภำพ (item21.java)
• จงเขียนโปรแกรมเพื่อรับข ้อมูลรหัสพนักงำน ชื่อ นำมสกุล เงินเดือน จำนวน
ปีที่ทำงำนจำกหน้ำจอภำพ แล ้วใช ้คำสั่ง switch คำนวณหำโบนัสและยอด
รวมของเงินที่พนักงำนจะได ้รับในเดือนนั้น โดยพนักงำนที่ทำงำนมำแล ้ว
3 ปี จะได ้รับโบนัสในอัตรำ 3% ของเงินเดือน พนักงำนที่ทำงำนมำมำกกว่ำ
3 ปี แต่ไม่เกิน 5 ปี จะได ้รับโบนัสในอัตรำ 5% ของเงินเดือน พนักงำนที่
ทำงำนมำมำกกว่ำ 5 ปี แต่ไม่เกิน 7 ปี จะได ้รับโบนัสในอัตรำ 10% ของ
เงินเดือน และพนักงำนที่ทำงำนมำตั้งแต่ 7 ปีขึ้นไป จะได ้รับโบนัสในอัตรำ
15% ของเงินเดือน (item22.java)
27
• จงเขียนโปรแกรมเพื่อแสดงจำนวนธนบัตรที่ได ้ หำกนำเงินที่
ผู้ใช ้รับจำกแป้นพิมพ์ไม่เกิน 100,000 บำท ไปแลกธนบัตรย่อย
(1000, 500, 100 บำท) และเหรียญ (10, 5, 1 บำท)
(item23.java)
• จงเขียนโปรแกรมเพื่อหำค่ำของ prime number ตั้งแต่ 1 ถึง
100 โดย prime number คือ เลข 1 และตัวของมันเท่ำนั้นที่
หำรลงตัว(item24.java)
ส่ง source code ในกระดำษ
ซึ่งมีกำรใช ้comment ระบุรหัสนิสิต ชื่อนิสิต และหมู่ที่เรียน
และ attach file java มำที่ lekchuti@gmail.com
ภำยในพฤหัสบดีที่ 20 ธันวำคม 2555

More Related Content

Similar to Computer Programming 3

Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคTheeravaj Tum
 
การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา CWarawut
 
Java week2
Java week2Java week2
Java week2Asa Thai
 
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Thanachart Numnonda
 
Computer programming
Computer  programmingComputer  programming
Computer programmingPreaw Jariya
 
Computer programming
Computer programmingComputer programming
Computer programmingJariyaa
 

Similar to Computer Programming 3 (20)

Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
Java-Answer Chapter 05-06
Java-Answer Chapter 05-06Java-Answer Chapter 05-06
Java-Answer Chapter 05-06
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 
Tec4
Tec4Tec4
Tec4
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบค
 
การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา C
 
05 loops
05 loops05 loops
05 loops
 
08 arrays
08 arrays08 arrays
08 arrays
 
Control structure
Control structureControl structure
Control structure
 
..Arrays..
..Arrays....Arrays..
..Arrays..
 
1 test
1  test1  test
1 test
 
Java week2
Java week2Java week2
Java week2
 
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
 
C language
C languageC language
C language
 
C language
C languageC language
C language
 
Array
ArrayArray
Array
 
Array 2
Array 2Array 2
Array 2
 
Computer programming
Computer  programmingComputer  programming
Computer programming
 
Computer programming
Computer programmingComputer programming
Computer programming
 
Python101
Python101Python101
Python101
 

More from Saranyu Srisrontong

More from Saranyu Srisrontong (11)

Computer Programming 1
Computer Programming 1Computer Programming 1
Computer Programming 1
 
Lab Computer Programming 1
Lab Computer Programming 1Lab Computer Programming 1
Lab Computer Programming 1
 
Computer Programming 4
Computer Programming 4Computer Programming 4
Computer Programming 4
 
Ac current46
Ac current46Ac current46
Ac current46
 
electric potential
electric potentialelectric potential
electric potential
 
พลังงานไฟฟ้า
พลังงานไฟฟ้าพลังงานไฟฟ้า
พลังงานไฟฟ้า
 
Physics2 1
Physics2 1Physics2 1
Physics2 1
 
Intellec.pro for final exam
Intellec.pro for final examIntellec.pro for final exam
Intellec.pro for final exam
 
การแก้ปัญหาการออกแบบและพัฒนาขั้นตอนวิธี
การแก้ปัญหาการออกแบบและพัฒนาขั้นตอนวิธีการแก้ปัญหาการออกแบบและพัฒนาขั้นตอนวิธี
การแก้ปัญหาการออกแบบและพัฒนาขั้นตอนวิธี
 
Network fundamental
Network fundamentalNetwork fundamental
Network fundamental
 
Computer systemarchitecture
Computer systemarchitectureComputer systemarchitecture
Computer systemarchitecture
 

Computer Programming 3

  • 1. 1 คำสั่ง if • simple if รูปแบบ if (เงื่อนไข) { คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นจริง; } ตัวอย่ำง if ( radius >= 0 ) { area = radius * radius * PI; System.out.println (“พื้นที่วงกลม คือ ” + area); } if (( i >= 0 ) and ( i <= 10 )) { System.out.println ( “i เป็นตัวเลขระหว่ำง 0 ถึง 10” ); } if ( sex == ‘m’ || sex == ‘M’ ) return height-100;
  • 2. 2 • if..else รูปแบบ if (เงื่อนไข) { คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นจริง; } else { คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นเท็จ; } ตัวอย่ำง if ( radius >= 0 ) { area = radius * radius * PI; System.out.println (“พื้นที่วงกลม คือ ” + area); } else { System.out.println (“ตัวเลขเป็นค่ำลบไม่ได ้”); } if ( sex == ‘m’ || sex == ‘M’ ) { return height-100; } else { return height-110; }
  • 3. 3
  • 4. 4 • Nested if แบบที่ 1 if (เงื่อนไข1) { if (เงื่อนไข2) { if (เงื่อนไข3)  } } else { คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นเท็จ; } แบบที่ 2 if (เงื่อนไข1) { คำสั่งเมื่อเงื่อนไข1 เป็นจริง; } else if (เงื่อนไข2) { คำสั่งเมื่อเงื่อนไข2 เป็นจริง; } else if (เงื่อนไข3) { คำสั่งเมื่อเงื่อนไข3 เป็นจริง; } 
  • 5. 5 ตัวอย่ำง if ( i > k ) { if ( j > k ) System.out.println (“i และ j มีค่ำมำกกว่ำ k”); } else { System.out.println (“i มีค่ำน้อยกว่ำหรือเท่ำกับ k”); } if ( score >= 90.0 ) { grade = ‘A’; } else if (score >= 80.0 ) { grade = ‘B’; } else if ( score >= 70.0 ) { grade = ‘C’;} else { grade = ‘F’; }
  • 6. 6 import java.io.*; class TestIf { public static void main(String[] args) throws IOException { BufferedReader Datain =new BufferedReader (new InputStreamReader (System.in)); System.out.print("Please key your score => "); String input = Datain.readLine(); int score = Integer.parseInt(input); System.out.println("Your input data is => " + score); char grade; if (score >= 90.0 ) { grade = 'A'; } else if (score >= 80.0 ) { grade = 'B'; } else if (score >= 70.0 ) { grade = 'C'; } else { grade = 'F'; } System.out.println("Your grade is => " + grade); } }
  • 7. 7
  • 8. 8 • Shortcut if รูปแบบ ตัวแปร = (เงื่อนไข)? คำสั่งเมื่อเงื่อนไขเป็นจริง : คำสั่งเมื่อเงื่อนไขเป็นเ็็จ ตัวอย่ำง if ( x > 0 ) y = 1 else y = -1; หรือ y = ( x > 0 ) ? 1 : -1 class Ternary { public static void main(String[] args) { int a,b; a = 10; b = a < 10 ? -a : a; System.out.println("Absolute value of a is " + b); a = -10; b = a < 10 ? -a : a; System.out.println("Absolute value of a is " + b); } }
  • 9. 9 1. จงแสดงผลลัพธ์จำกกำรทำงำนของโปรแกรมต่อไปนี้ (item1.java) public class three { public static void main(String args[]) { int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6; int ans1 = c * c + c % b; int ans2 = b + e / c - c * d; int ans3 = b * (a - (d / e) / b) * (b - e % c); int ans4 = a + b - c / d / e * f; System.out.println("ans1 is " + ans1 + " และ " + "ans2 is " + ans2); System.out.println("ans3 is " + ans3 + " และ " + "ans4 is " + ans4); } } แบบฝึกหัด ครั้งที่ 1
  • 10. 10 2. จงแสดงผลลัพธ์จำกกำรทำงำนของโปรแกรมต่อไปนี้ (item2.java) public class four { public static void main(String args[]) { boolean a = true, b = true, c = true; boolean ans1 = !a && b; boolean ans2 = a && b || c; boolean ans3 = a || (b && c); boolean ans4 = a && b || c; System.out.print("ans1 is " + ans1 + " และ "); System.out.println("ans2 is " + ans2); System.out.print("ans3 is " + ans3 + " และ "); System.out.println("ans4 is " + ans4); } }
  • 11. 11 3. จงเขียนโปรแกรมเพื่อคำนวณผลลัพธ์ของนิพจน์ต่อไปนี้ 5 + 1 / 7 3 * 3 + 3 % 2 2 + 5 / 3 + -3 * 4 2 * (1 + -(4 / 5) / 2) * (2 - 5 % 3) 4. จงเขียนโปรแกรมเพื่อตรวจสอบผลกำรคำนวณของนิพจน์ต่อไปนี้ a+b*c!=b/c%2&&a*b/--c||b++/a>0 a*b/c==b>0||a%b+--a/c++
  • 12. 12 คำสั่ง Switch รูปแบบ switch (ตัวแปร) { case ค่ำที่ 1 : คำสั่งที่ 1; break; case ค่ำที่ 2 : คำสั่งที่ 2; break;  case ค่ำที่ N : คำสั่งที่ N; break; default : คำสั่งเมื่อไม่มีค่ำที่ตรงกับค่ำที่ระบุใน case; }
  • 13. 13 ตัวอย่ำง switch (year) { case 5 : IRATE = 12; break; case 15 : IRATE = 18; break; case 30 : IRATE = 24; break; default : System.out.println(“ตัวเลขผิด โปรดแก ้ไข”);}
  • 14. 14
  • 15. 15 คำสั่ง While รูปแบบ while (เงื่อนไข) { คำสั่งต่ำง ๆ เมื่อเงื่อนไขเป็นจริง; } ตัวอย่ำง while (Number >= 1.0) { Number *= 0.1; TimesMoved++; }
  • 16. 16
  • 17. 17 คำสั่ง Do..While รูปแบบ do { คำสั่งต่ำง ๆ; } while (เงื่อนไข); ตัวอย่ำง do { data = data + 1; sum += data; } while (data <= 10);
  • 18. 18
  • 19. 19 คำสั่ง For รูปแบบ for (ค่ำตัวแปรเริ่มต ้น ; เงื่อนไข ; เปลี่ยนแปลงค่ำตัวแปร) ตัวอย่ำง int i; for ( i = 0 ; i < 100 ; i++ ) { System.out.println (“ Warm Welcome”); } for ( i = 0; i < 10; i++ ) { for ( j = i; j < 10; j++ ) System.out.print( " * " ); System.out.println( ); }
  • 20. 20 import java.io.*; public class Reverse { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); System.out.println("Please key your text "); String s = stdin.readLine( ); String r = " "; for (int i = 0; i < s.length( ); i++) { char ch = s.charAt(i); r = ch + r; System.out.println (r); } System.out.println (s + " Reverse of your text is " + r); } }
  • 21. 21 คำสั่ง Break และ Continue • Break หำกโปรแกรมพบคำสั่งนี้จะหลุดออกจำก Loop กำร ทำงำนทันที • Continue หำกโปรแกรมพบคำสั่งนี้จะหยุดกำรทำงำนที่จุด นั้น แล ้วย ้อนกลับไปเริ่มต ้นกำรทำงำนที่ Loop ใหม่ ตัวอย่ำง while (1 < 2) { Number = Number + 1; if ( Number == 10 ) { System.out.println (" จบกำรทำงำน"); break; } if ( Number > 0 && Number <= 9) { System.out.println (" ตัวเลข " + Number ); continue; } }
  • 22. 22 outer : for (int i = 0; i < 3; i++ ) { System.out.print (" Pass " + i + " : " ); for (int j = 0; j < 10; j++ ) { System.out.print ( j+ " " ); } break outer; } outer : for (int i = 0; i < 10; i++ ) { for (int j = 0; j < 10; j++ ) { if (j > i) { System.out.println ( ); continue outer; } System.out.print (" " + ( i*j )); } }
  • 23. 23 public class breaklabel { public static void main(String[] args) { outer : for (int i = 0; i < 3; i++ ) { System.out.print (" Pass " + i + " : " ); for (int j = 0; j < 10; j++ ) { System.out.print ( j+ " " ); } break outer; } System.out.println (" Loops complete. " ); } }
  • 24. 24 public class continuelabel { public static void main(String[] args) { outer : for (int i = 0; i < 10; i++ ) { for (int j = 0; j < 10; j++ ) { if (j > i) { System.out.println ( ); continue outer; } System.out.print (" " + ( i*j )); } } System.out.println ( ); } }
  • 25. 25 public class Table1 { public static void main (String[] args) { final int COLUMN_WIDTH = 8; for (int x = 1; x <= 5; x++) { for (int y = 1; y <= 4; y++) { int p = (int)Math.pow(x,y); String pstr = " " + p; while (pstr.length() < COLUMN_WIDTH) pstr = " " + pstr; System.out.print(pstr); } System.out.println(); } } }
  • 26. 26 แบบฝึกหัดที่ 1 • จงเขียนโปรแกรมเพื่อรับข ้อมูลชื่อและนำมสกุล และวันเดือนปีเกิดของผู้ใช ้ จำกหน้ำจอภำพ แล ้วสร ้ำงรหัสชุดหนึ่งประกอบด ้วยตัวอักษร 5 ตัว ตัวแรก ได ้มำจำกอักษรแรกของชื่อผู้ใช ้ตัวที่สองได ้มำจำกควำมยำวของชื่อผู้ใช ้ ตัวที่สำมและสี่ได ้มำจำกรหัส ASC II ของอักษรแรกของนำมสกุลผู้ใช ้และ ตัวที่ห ้ำได ้มำจำกอักษรตัวสุดท ้ำยของนำมสกุลผู้ใช ้จำกนั้นให ้แสดง แสดงผลรหัสชุดดังกล่ำวและอำยุจำนวนกี่ปีและกี่เดือนของผู้ใช ้ณ วันที่ 12 ธ.ค. 55 หน้ำจอภำพ (item21.java) • จงเขียนโปรแกรมเพื่อรับข ้อมูลรหัสพนักงำน ชื่อ นำมสกุล เงินเดือน จำนวน ปีที่ทำงำนจำกหน้ำจอภำพ แล ้วใช ้คำสั่ง switch คำนวณหำโบนัสและยอด รวมของเงินที่พนักงำนจะได ้รับในเดือนนั้น โดยพนักงำนที่ทำงำนมำแล ้ว 3 ปี จะได ้รับโบนัสในอัตรำ 3% ของเงินเดือน พนักงำนที่ทำงำนมำมำกกว่ำ 3 ปี แต่ไม่เกิน 5 ปี จะได ้รับโบนัสในอัตรำ 5% ของเงินเดือน พนักงำนที่ ทำงำนมำมำกกว่ำ 5 ปี แต่ไม่เกิน 7 ปี จะได ้รับโบนัสในอัตรำ 10% ของ เงินเดือน และพนักงำนที่ทำงำนมำตั้งแต่ 7 ปีขึ้นไป จะได ้รับโบนัสในอัตรำ 15% ของเงินเดือน (item22.java)
  • 27. 27 • จงเขียนโปรแกรมเพื่อแสดงจำนวนธนบัตรที่ได ้ หำกนำเงินที่ ผู้ใช ้รับจำกแป้นพิมพ์ไม่เกิน 100,000 บำท ไปแลกธนบัตรย่อย (1000, 500, 100 บำท) และเหรียญ (10, 5, 1 บำท) (item23.java) • จงเขียนโปรแกรมเพื่อหำค่ำของ prime number ตั้งแต่ 1 ถึง 100 โดย prime number คือ เลข 1 และตัวของมันเท่ำนั้นที่ หำรลงตัว(item24.java) ส่ง source code ในกระดำษ ซึ่งมีกำรใช ้comment ระบุรหัสนิสิต ชื่อนิสิต และหมู่ที่เรียน และ attach file java มำที่ lekchuti@gmail.com ภำยในพฤหัสบดีที่ 20 ธันวำคม 2555