一、作业要求:
p59 习题7,求圆和梯形面积
二、源码分析
2.1 one.jsp
<%--
Created by IntelliJ IDEA.
User: corrine
Date: 2019/3/26
Time: 12:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tra" %>
<%@page import="java.text.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>请输入三角形三边长:
<form action="" method="post" name=form>
<input type="text" name="a" id="length1">
<BR><input type="text" name="b" id="length2">
<BR><input type="text" name="c" id="length3">
<BR><Input type="submit"value="送出" name=submit1>
</form>
<%String a=request.getParameter("a");//取值并转化为字符串
String b=request.getParameter("b");
String c=request.getParameter("c");
%>
<tra:GetArea7 a="<%=a %>" b="<%=b %>" c="<%=c %>"/>//通过attribute传参
<% NumberFormat f=NumberFormat.getInstance();
f.setMaximumFractionDigits(3);//设置保留三位小数
if(area!=null)
{double d=area.doubleValue();
String str=f.format(d);
out.println(str);}
%>
</body>
</html>
2.2 two.jsp
<%--
Created by IntelliJ IDEA.
User: corrine
Date: 2019/3/26
Time: 12:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tra" %>
<%@page import="java.text.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>请输入三角形三边长:
<form action="" method="post" name=form>
<input type="text" name="a" id="length1">
<BR><input type="text" name="b" id="length2">
<BR><input type="text" name="c" id="length3">
<BR><Input type="submit"value="送出" name=submit1>
</form>
<%String a=request.getParameter("a");
String b=request.getParameter("b");
String c=request.getParameter("c");
%>
<tra:GetArea7 b="<%=b %>" a="<%=a %>" c="<%=c %>"/>
<% NumberFormat f=NumberFormat.getInstance();
f.setMaximumFractionDigits(6); //将参数改为6即可保留6位小数
if(area!=null)
{double d=area.doubleValue();
String str=f.format(d);
out.println(str);}
%>
</body>
</html>
2.3 GetArea7.tag
<%@tag import="java.util.*" %>
<%@attribute name="a" required="true" %>
<%@attribute name="b" required="true" %>
<%@attribute name="c" required="true" %>
<%@variable name-given="area" variable-class="java.lang.Double" scope="AT_END"%>
<%
if(a!=null&&b!=null&&c!=null){
double numberA=Double.parseDouble(a);
double numberB=Double.parseDouble(b);
double numberC=Double.parseDouble(c);
if(numberA+numberB>numberC&&numberB+numberC>numberA&&numberA+numberC>numberB){
double p=(numberA+numberB+numberC)/2.0;
double area=Math.sqrt(p*(p-numberA)*(p-numberB)*(p-numberC));
if(a!=null&&b!=null&&c!=null){
jspContext.setAttribute("area",new Double(area));
}
}
}
%>
三、结果图