Page 60 - การโปรแกรมเว็บ
P. 60

7-48 การโปรแกรมเวบ็
       รปู แบบค�ำสงั่ switch

 switch(<expression>) {
 	 case val :
 		 statement1;
 		 break;
 	 case val :
 		 statement2;
 		 break;
 	 case val :
 		 statement3;
 		 break;
 	 [default:]
 }

       ตัวอย่างท่ี 7.18 การใช้ค�ำสง่ั switch

 MyJSP8_switch_1.jsp
 โปรแกรมนเ้ี ป็นตวั อย่างการใชค้ ำ� สั่ง switch ในการตัดสนิ ใจเลอื กการข้นึ -ลงลิฟตไ์ ปยังชน้ั ตา่ งๆ

    1 <%@page contentType="text/html" pageEncoding="UTF-8"%>
   2 <!DOCTYPE html>
   3 <html>
   4 	 <head>
   5 		 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   6 		 <title>JSP Page</title>
   7 	 </head>
   8 	 <body>
   9 		 <%
   10 		 char floor ='2';
   11 		 out.println("What's floor do you want to go: " + floor + "<br>");
   12 		 switch (floor) {
   13 			 case 'G' :
   14 				 out.println("Elevator is going to ground floor.");
  15 				 break;
   55   56   57   58   59   60   61   62   63   64   65