JSP——通过表单main.jsp向triangle.jsp提交三角形三边的长度,triangle.jsp计算并显示三角形的面积

1.main.jsp文件代码

<%@page contentType="text/html;charset=utf-8"%>
<html>
    <head>输入三角形的的三边长,求面积</head>
    <body>
        <form action="triangle.jsp" method=post>
            <input type="text" name="sizeA" >
            <input type="text" name="sizeB" >
            <input type="text" name="sizeC" >
            <input type="submit" name="submit" value="提交">
        </form>
    </body>    
</html>

2.triangle.jsp文件代码

<%@page contentType="text/html;charset=utf-8"%>
<html>
    <body>
<%
    String sizeA=request.getParameter("sizeA");
    String sizeB=request.getParameter("sizeB");
    String sizeC=request.getParameter("sizeC");
    double a =Double.parseDouble(sizeA);
    double b=Double.parseDouble(sizeB);
    double c=Double.parseDouble(sizeC);
    double p=(a+b+c)/2,area=0;
    area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
    out.println("<br>三角形的面积:"+area);
 %>
        
    </body>
</html>

3.浏览器的运行结果

猜你喜欢

转载自blog.csdn.net/lmm0513/article/details/88995619