jsp--三角形面积计算

时间:2024-03-28 08:45:50

GetArea.tag

<%@ tag import="java.text.DecimalFormat" %>
<%@tag pageEncoding="UTF-8"%>
<%@attribute name="numberA" required="true"%>
<%@attribute name="numberB" required="true"%>
<%@attribute name="numberC" required="true"%>
<%@attribute name="radio" required="true"%>
<%@variable name-given="area" variable-class="java.lang.Double" scope="AT_END"%>
<%@variable name-given="df" variable-class="java.text.DecimalFormat" scope="AT_END"%>
<%@variable name-given="message"  scope="AT_END"%>
<%!
    public double getArea(double a,double b,double c) {
        if (a + b > c && a + c > b && b + c > a ) {
            double p = (a + b + c)/2.0;
            double area = Math.sqrt(p*(p-a)*(p-b)*(p-c));
            return area;
        }
        return -1;
    }

    public double change(double num,String str) {
        DecimalFormat df = new DecimalFormat("#." + str);
        String format = df.format(num);
        double value = Double.valueOf(format);
        return  value;
    }
%>

<%
    try {
        double a = Double.parseDouble(numberA);
        double b = Double.parseDouble(numberB);
        double c = Double.parseDouble(numberC);
        double result = 0;
        result = getArea(a,b,c);
        if (radio.equals("three")) {
            double result1 = change(result, "000");
            jspContext.setAttribute("area", result1);
            jspContext.setAttribute("message", "三角形面积:");
        }

        if (radio.equals("six")) {
            double result1 = change(result, "000000");
            jspContext.setAttribute("area", result1);
            jspContext.setAttribute("message", "三角形面积:");
        }
    } catch (Exception e) {
        jspContext.setAttribute("area",new Double(-1.0));
        jspContext.setAttribute("message",""+e.toString());
    }
%>

one.jsp

<%--
  Created by IntelliJ IDEA.
  User: Hotin
  Date: 2019/3/28
  Time: 10:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag"%>
<html>
  <head>
    <title>one.jsp</title>
  </head>
  <body bgcolor="#6495ed">
  <form action="" method="post">
    <table>
      <th>one</th>
      <tr>
        <td>输入数值A:</td>
        <td><input type="text" name="a" placeholder="请输入数值a..."></td>
      </tr>

      <tr>
        <td>输入数值B:</td>
        <td><input type="text" name="b" placeholder="请输入数值b..."></td>
      </tr>

      <tr>
        <td>输入数值C:</td>
        <td><input type="text" name="c" placeholder="请输入数值c..."></td>
      </tr>
      <tr>
        <td>格式:</td>
        <td><input type="radio" name="rd" value="three">0.000</td>
        <td><input type="radio" name="rd" value="six">0.000000</td>
      </tr>
      <tr>
        <td><input type="submit" name="sb" value="提交"></td>
      </tr>
    </table>
  </form>

  <%
    String a = request.getParameter("a");
    String b = request.getParameter("b");
    String c = request.getParameter("c");
    String r = request.getParameter("rd");
    if (a == null || b == null || c == null) {
        a = "0";
        b = "0";
        c = "c";
    }
    if (a.length() > 0 && b.length() > 0 && c.length() >0) { %>
    <tag:GetArea numberA="<%=a%>" numberB="<%=b%>" numberC="<%=c%>" radio="<%=r%>"/> <br>
    <table>
        <tr>
          <td><%=message%></td>
          <td><%=area%></td>
        </tr>
    </table>
  <%}%>
  </body>
</html>

运行结果

  • 小数点后三位数运算结果
    jsp--三角形面积计算
    -小数点后六位数运算结果
    jsp--三角形面积计算