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); } }
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(); } } }