BMI.jsp
<%@ page contentType="text/html; charset=big5"%>
<form method="post" action="BMI.jsp">
性別:<select name="sex">
<option></option>
<option value="man">男</option>
<option value="woman">女</option>
</select><br>
身高(公分):<input type="text" name="height" style="width:5%"/><br>
體重(公斤):<input type="text" name="weight" style="width:5%"/><br>
<input type="submit" value="送出">
</form>
<%
String height = request.getParameter("height");
String weight = request.getParameter("weight");
if(height == null || weight == null){
height = "0.0";
weight = "0.0";
}
String url = "count.jsp?height=" + height + "&weight=" + weight;
%>
<jsp:include page="<%=url%>"></jsp:include>
count.jsp
<%@ page contentType="text/html; charset=big5" %>
<%@
page import="java.io.*"
%>
<%
String exponent = "";
String standard = "";
int num = 0;
double h=0.0,w=0.0;
h = Double.parseDouble(request.getParameter("height")) / 100;
w = Double.parseDouble(request.getParameter("weight"));
double ans = w / (Math.pow(h, 2)); //體重 (kg) / 身高 (m)的平方
if( Double.isNaN(ans) == false){
exponent = Long.toString(Math.round(ans));
num = Integer.parseInt(exponent);
/*
男性: (身高cm-80)×70﹪=標準體重
女性: (身高cm-70)×60﹪=標準體重
*/
if(request.getParameter("sex").equals("man")){
standard = Double.toString(((h*100)-80)*0.7) + " 公斤";
}else{
standard = Double.toString(((h*100)-70)*0.6) + " 公斤";
}
}
String str = "";
//結果
if( num == 0){
str = "";
}else if( num < 18.5 ){
str = "體重過輕";
}else if( num > 18.5 && num < 24){
str = "正常範圍";
}else if( num >= 24 && num < 27){
str = "體重過重";
}else if( num >= 27 && num < 30){
str = "輕度肥胖";
}else if( num >= 30 && num < 35 ){
str = "中度肥胖";
}else{
str = "重度肥胖";
}
%>
<p>BMI 指數:<%=exponent%></p>
<p>結果:<%=str%></p>
<p>標準體重:<%=standard%></p>
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>BMI.jsp</welcome-file>
</welcome-file-list>
</web-app>
沒有留言:
張貼留言