jsp:计算三角形面积,结果保留3,6位小数

这篇博客介绍如何使用Java在jsp中计算三角形的面积,并通过设置NumberFormat保留指定的小数位数,例如3位或6位。用户输入三角形的三边长,程序会验证是否能构成三角形,然后计算并显示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

three.jsp(三位小数)

<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="java.text.*" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="computer" %>
<html>
<body>
<form action="" method=get name=form>
    <h3>请输入三角形三边长:</h3>
    <table>
        <tr>
            <td>a:</td>
            <td><input type="text" name="a"></td>
        </tr>
        <tr>
            <td>b:</td>
            <td><input type="text" name="b"></td>
        </tr>
        <tr>
            <td>c:</td>
            <td><input type="text" name="c"></td>
        </tr>
    </table>
    <br> <input type="submit" value="计算结果最多保留3位小数" name=submit>
    <% String a=request.getParameter("a");
    String b=request.getParameter("b");
    String c=request.getParameter("c");
    if(a==null||b==null||c==null){
        a="0";
        b="0";
        c="0";
    }
    if(a.length()>0&&b.length()>0&&c.length()>0){
%>
        <computer:GetArea sideA="<%=a %>" sideB="<%=b %>" sideC="<%=c %>" />
<%
        NumberFormat f=NumberFormat.getInstance();
        f.setMaximumFractionDigits(3);
        double result=area.doubleValue();
        String str=f.format(result);
        out.println(str);
    } %>
</form>
</body>
</html>

六位小数 f.setMaximumFractionDigits(6);

 

Area.tag:(计算三角形面积)

<%@ tag pageEncoding="gb2312" %>
<%@ attribute name="sideA" required="true" %>
<%@ attribute name="sideB" required="true" %>
<%@ attribute name="sideC" required="true" %>
<%@ variable name-given="area" variable-class="java.lang.Double" scope="AT_END" %>
<%
    double a=Double.parseDouble(sideA);
    double b=Double.parseDouble(sideB);
    double c=Double.parseDouble(sideC);
    if(a+b>c&&a+c>b&&c+b>a){
        double p=(a+b+c)/2.0;
        double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
        jspContext.setAttribute("area",new Double(area));
    }
    else{
        jspContext.setAttribute("area",new Double(-1));
    }
%>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值