Page 85 - การโปรแกรมเว็บ
P. 85
เจเอสพี 7-73
ผลการท�ำงานของโปรแกรมแสดงดังภาพที่ 7.29
ภาพที่ 7.29 ผลการท�ำงานของโปรแกรมค�ำนวณพ้ืนท่ีสามเหล่ียม
ตัวอย่างท่ี 7.31 การเขียนฟงั กช์ ันสำ� หรบั คำ� นวณพน้ื ทีท่ รงกระบอก
MyJSP_Cylinder.jsp
โปรแกรมน้เี ป็นการเขยี นฟงั ก์ชนั เพือ่ หาคา่ พน้ื ท่ีทรงกระบอก พรอ้ มทง้ั แสดงผลลัพธ์บนหน้าเว็บ
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 : Cylinder</title>
7 </head>
8 <body>
9 <%! double Cylinder(double radius, double high) {
10 return 3.14 * Math.pow(radius, 2) * high;
11 } %>
12 <h3> โปรแกรมคำ� นวณหาพ้นื ท่ีทรงกระบอก </h3>
13 <hr>
14 <h3> Cylinder 1 is : <% out.print(Cylinder (20, 80)); %> </h3>
15 <h3> Cylinder 2 is : <% out.print(Cylinder (36, 120)); %> </h3>
16 <h3> Cylinder 3 is : <% out.print(Cylinder (12, 70)); %> </h3>
17 </body>
18 </html>