Computer Programming 3
-
Upload
saranyu-srisrontong -
Category
Education
-
view
28 -
download
1
Transcript of Computer Programming 3
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; }
4
• Nested ifแบบที่ 1
if (เงือ่นไข1){ if (เงือ่นไข2)
{ if (เงือ่นไข3)
}}else{ ค ำสัง่ตำ่ง ๆ เมือ่เงือ่นไขเป็นเท็จ; }
แบบที่ 2if (เงือ่นไข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); } }
8
• Shortcut ifรูปแบบตวัแปร = (เงือ่นไข)? ค ำส ั่งเมือ่เงือ่นไขเป็นจรงิ : ค ำส ั่งเมือ่เงือ่นไขเป็นเ็็จ
ตวัอยำ่ง if ( x > 0 ) y = 1else 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 / 73 * 3 + 3 % 22 + 5 / 3 + -3 * 42 * (1 + -(4 / 5) / 2) * (2 - 5 % 3)
4. จงเขยีนโปรแกรมเพือ่ตรวจสอบผลกำรค ำนวณของนพิจนต์อ่ไปนี้a+b*c!=b/c%2&&a*b/--c||b++/a>0a*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(“ตวัเลขผดิ โปรดแกไ้ข”);}
15
ค ำสัง่While
รปูแบบ
while (เงือ่นไข)
{ ค ำสัง่ตำ่ง ๆ เมือ่เงือ่นไขเป็นจรงิ; }
ตวัอยำ่ง
while (Number >= 1.0)
{ Number *= 0.1;
TimesMoved++; }
17
ค ำสัง่Do..While
รปูแบบ
do
{ ค ำสัง่ตำ่ง ๆ; }
while (เงือ่นไข);
ตวัอยำ่ง
do
{ data = data + 1;
sum += data; }
while (data <= 10);
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 มำที ่[email protected]ภำยในพฤหสับดทีี ่20 ธนัวำคม 2555