gpt4 book ai didi

java - Java Web 应用程序中的 BMI 计算器

转载 作者:行者123 更新时间:2023-12-01 14:28:27 26 4
gpt4 key购买 nike

我需要用java计算bmi到目前为止我有这段代码

input_page.jsp

<form action="calc_bmi.jsp" method="post">
<table>
<tr>
<td>height:</td>
<td><input type="text" name="height" size="35" maxlength="6" ></td>
</tr>
<tr>
<td>weight:</td>
<td><input type="text" name="weight" size="35" maxlength="6" ></td>
</tr>

<tr>
<td colspan="2"><input type="button" onclick="formReset()" value="reset">
<input type="submit" value="submit"></td>
</tr>
</table>
</form>

calc_bmi.jsp

<%
String wt=request.getParameter("weight");
String ht=request.getParameter("height");

double weight=Integer.parseInt(wt);
double height=Integer.parseInt(ht);

double w=weight;
double h=height*height;
double bmi = w/h;
out.println("BMI of weight "+weight+" and height "+height+" is: "+bmi);
%>

我如何才能接受体重和高度字段中的小数?

谢谢

最佳答案

很简单,

使用Double代替Integer

喜欢:

双权重=Double.parseDouble(wt);

此外,我建议您在提交表单之前或在服务器端解析数据时使用java脚本进行一些验证,否则可能会抛出NumberFormatException

关于java - Java Web 应用程序中的 BMI 计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16998505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com