<html>
<head><title>计算一个人每天的卡路里消耗需求量</title>
<script language = "JavaScript">
function isEmpty(key)
{
if(key.value==""||key.value==null)return true;
else return false;
}
function OnSubmit()
{   var age;
if(isEmpty(document.myform.age))    
 {
alert("请输入您的年龄");
document.myform.age.focus();
return false;
  }
 if(isEmpty(document.myform.weight))
 {
alert("请输入您的体重");
document.myform.weight.focus();
return false;
 }
 if(isEmpty(document.myform.h_m))
 {
alert("请输入您的身高(m)");
document.myform.h_m.focus();
return false;
 }
if(isEmpty(document.myform.h_cm))
 {
alert("请输入您的身高(cm)");
document.myform.h_cm.focus();
return false;
 }
 return true;
}
</script>
</head>
<body>
<form name = "myform" Action="calculate.jsp" Method = "post"  OnSubmit="return OnSubmit()" >
<br><br><br><br><br><br>
<table align = "center">
<tr>
<td colspan = "2" align = "center">
<font size = 5 color = "green" >请输入您的信息</font>
<br><br>
</td>
</tr>
<tr>
    <td align = "center">性别:</td>
<td align = "center">
<input type = "radio" name = "sex" value = "male" checked >男
<input type = "radio" name = "sex" value = "female">女
</td>
</tr>
<tr>
<td align = "center">年龄:</td>
<td align = "center">
<input type = "text" name = "age" size = "12" value = "">
</td>
</tr>
<tr>
<td align = "center">体重:</td>
<td align = "center">
<input type = "text" name = "weight" size = "12" value = "">
</td>
</tr>
<tr>
<td align = "center">身高:</td>
<td align = "center">
<input type = "text" name = "h_m" size = "3" value = "">m
<input type = "text" name = "h_cm" size = "3" value = "">cm
</td>
</tr>
<tr><td><br><br></td></tr>
<tr>
<td colspan ="2" align = "center">
<font size = 5 color = "green" >生活习惯<br></font>
</td>
</tr>
<tr>
<td colspan ="2">
<input type = "radio" name = "lifestyle" value="style1" checked>
                    1:在大多数时间,你在坐着,读书,敲键盘,或主要从事计算机工作
</td>
</tr>
<tr>
<td colspan ="2">
<input type = "radio" name = "lifestyle" value="style2">
2:你参加轻量锻炼,譬如每日行走小于2个小时。
</td>
</tr>
<tr>
<td colspan ="2">
<input type = "radio" name = "lifestyle" value="style3">
3:你白天很少坐下并从事繁重家务劳动,从事园艺,或每日跳舞
</td>
</tr>
<tr>
<td colspan ="2">
<input type = "radio" name = "lifestyle" value="style4">
4:你参加体育活动,譬如跑步或登山,或者你是建筑工人或自行车邮递员。
</td>
</tr>
<tr>
<td>
<input  type = "submit" value = "计算每天的卡路里消耗量">
</td>
</tr>
</table>
</form>
</html>

解决方案 »

  1.   


    package callbean;
    /**
     *Calculate.java文件 
     * @author wxy
     *卡路里消耗需求量的计算公式如下:
     *fltBMR =(体重*4.4 + 身高*1.85 - 年龄*4.7  + 655)   (男性,身高单位为厘米)
     *fltBMR =(体重*6.2 + 身高*5 - 年龄*6.8  + 666)     (女性,身高单位为厘米)
     *fltCPA = fltBMR * activity    (每种生活习惯对应的activity不同,图中四种习惯的activity系数分别为0.20、0.30、0.40、0.50)
     *fltEDA = (fltBMR + fltCPA)*0.10
     *fltTEN = fltBMR + fltCPA + fltEDA   (计算结果)
     */
    public class Calculate {
    private double weight, fltBMR,fltCPA,fltEDA,fltTEN;
    private  int height,age;
    private String lifestyle,sex;
    public Calculate()
    {
    // this.age=0;
    // this.height=0;
    // this.weight=0;
    // this.lifestyle=null;
    // this.sex=null;
    // this.fltBMR=0;
    // this.fltCPA=0;
    // this.fltEDA=0;
    // this.fltTEN=0;
    }
    /* Calculate(int age ,double w,int h,String s,String life)
    {
    this.age=age;
    this.height=h;
    this.weight=w;
    this.lifestyle=life;
    this.sex=s;
    }*/
    public void setAll(int age ,double w,int h,String s,String life)
    {
    this.age=age;
    this.height=h;
    this.weight=w;
    this.lifestyle=life;
    this.sex=s;
    }
    public void setAge(int a)
    {
    this.age=a;
    }
    public void setHeight(int h)
    {
    this.height=h;
    }
    public void setWeight(double w)
    {
    this.weight=w;
    }
    public void setSex(String s)
    {
    this.sex=s;
    }
    public void setLifeStyle(String life)
    {
    this.lifestyle=life;
    }
    public int getAge()
    {
    return this.age;
    }
    public int getHeight()
    {
    return this.height;
    }
    public double getWeight()
    {
    return this.weight;
    }
    public String getSex()
    {
    return this.sex;
    }
    public String getLifeStyle()
    {
    return this.lifestyle;
    }
    public double getFltCPA()
    {
    return this.fltCPA;
    }
    public double getFltBMR()
    {
    return this.fltBMR;
    }
    public double getFltEDA()
    {
    return this.fltEDA;
    }
    public double getFltTEN()
    {
    return this.fltTEN;
    }
    public void computeFltBMR()
    {
    //fltBMR =(体重*4.4 + 身高*1.85 - 年龄*4.7  + 655)   (男性,身高单位为厘米)
    //fltBMR =(体重*6.2 + 身高*5 - 年龄*6.8  + 666)     (女性,身高单位为厘米)
    if(this.getSex().compareTo("male")==0)
    {
    this.fltBMR=this.getWeight()*4.4+this.getHeight()*1.85-this.age*4.7+655;
    }
    else
    {
    this.fltBMR=this.getWeight()*6.2+this.getHeight()*5-this.age*6.8+665;
    }
    }
    public void computeCPA()
    {
    //fltCPA = fltBMR * activity    (每种生活习惯对应的activity不同,
    //图中四种习惯的activity系数分别为0.20、0.30、0.40、0.50)
    if(this.getLifeStyle().compareTo("style1")==0)
    {
    this.fltCPA=0.20*this.getFltBMR();
    }
    else if(this.getLifeStyle().compareTo("style2")==0)
    {
    this.fltCPA=0.30*this.getFltBMR();
    }
    else if(this.getLifeStyle().compareTo("style3")==0)
    {
    this.fltCPA=0.40*this.getFltBMR();
    }
    else this.fltCPA=0.5*this.getFltBMR();
    }
    public void computeFltEDA()
    {
    //fltEDA = (fltBMR + fltCPA)*0.10
    this.fltEDA=(this.getFltBMR()+this.getFltCPA())*0.10;
    }
    public void computeFltTEN()
    {
    //fltTEN = fltBMR + fltCPA + fltEDA   (计算结果)
    this.fltTEN=this.getFltBMR()+this.getFltCPA()+this.getFltEDA();
    }
    }
      

  2.   


    calculate.jsp文件的内容
    <%@ page language = "java" contentType = "text/html;charset = GBK" errorPage="error.jsp" %>
    <% request.setCharacterEncoding("GBK");%>
    <html>
    <head>
    <title>Result</title>
    </head>
    <body>
    <%
        int age=0;
    int h=0;
    String sex;
    String lifestyle;
    double weight;
    %>
    <%

    try{
    age = Integer.parseInt(request.getParameter("age")); 
    }
    catch(NumberFormatException nfex)
    {
    throw new NumberFormatException("输入的年龄不是整数");
    }
    try
    {
    h = Integer.parseInt(request.getParameter("h_m"))*100+Integer.parseInt(request.getParameter("h_cm"));
    }
    catch(NumberFormatException nfex)
    {
    throw new NumberFormatException("输入的身高不是整数");
    }
    try
    {
    weight = Double.parseDouble(request.getParameter("weight"));
    }
    catch(NumberFormatException nfex)
    {
    throw new NumberFormatException("输入的体重不是数字");
    }
    sex = request.getParameter("sex");
    lifestyle = request.getParameter("lifestyle");
    %>
    <jsp:useBean id = "test" scope = "page" class = "callbean.Calculate"/>
    <% test.setAll(age,weight, height, sex, lifestyle);%>
    <% test.computeFltBMR();test.computeCPA();test.computeFltEDA();test.computeFltTEN();%>
    <p><center>
    <font size = 5 color = "green">您一般需要<%=test.getFltTEN()%>卡能量</font>
    <hr>
            请注意,这个估计根据您的体重,身高,年龄,性别,和平均活动水平得出。
    <br>
    您能使用这些信息以帮助了解需要多少卡路里以维持您的体重。超过需求会使体重<br>
    增加,少于需求会使体重减轻。需要根据实际工作和锻炼情况决定饮食。<br>
    <br>
    <a href = "javascript:history.back();">计算其他人的热量需要</a>
    </center>
    </body>
    </html>
    错误的内容
    org.apache.jasper.JasperException: /calorie/calculate.jsp(43,2) The value for the useBean class attribute Calculate is invalid.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1200)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1160)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2393)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2399)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3372)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:294)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:281)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
      

  3.   

    <% 
        int age=0; 
    int h=0; 
    String sex; 
    String lifestyle; 
    double weight; 
    %> 
    先初始化了再说吧```
      

  4.   

    。jsp可以写这么多《%%》..先整整思路吧这种写法,就算出了错也没有人愿意帮你改