【jsp 练习】 给定三角形三边判断是否能组成三角形及计算面积

Test.java

package package1;

public class Test {
    double side1 = -1 , side2 = -1 , side3 = -1 , area = -1;
    boolean triangle;
    
    public double getSide1() {
        return side1;
    }
    public void setSide1(double side1) {
        this.side1 = side1;
    }
    public double getSide2() {
        return side2;
    }
    public void setSide2(double side2) {
        this.side2 = side2;
    }
    public double getSide3() {
        return side3;
    }
    public void setSide3(double side3) {
        this.side3 = side3;
    }
    public double getArea() {
        double p = (side1 + side2 + side3)/2.0;
        if(triangle) area = Math.sqrt( p * (p - side1) * (p - side2) * (p - side3) );
        return area;
        
    }
    public void setArea(double area) {
        this.area = area;
    }
    public boolean isTriangle() {
        if(side1<side2+side3 && side2<side1+side3 && side3<side1+side2)
            triangle=true;
        else triangle=false;
        return triangle;
    }
    public void setTriangle(boolean triangle) {
        this.triangle = triangle;
    }   
}

 

 

 

 

triangle.jsp

<%@page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %>
<%@page import="package1.Test" %>

<jsp:useBean id="tri" class="package1.Test" scope="page" />

<html>
    <head></head>
    <body>
        <form action="" method="post">
            a:<input type="text" name="side1" value="0" />
            b:<input type="text" name="side2" value="0" />
            c:<input type="text" name="side3" value="0" />
            <input type="submit" value="submit" />
        </form>
        
        <jsp:setProperty name="tri" property="*" />
        
        三角形的三边是:
        边a :<jsp:getProperty name="tri" property="side1" />
        边b :<jsp:getProperty name="tri" property="side2" />
        边c :<jsp:getProperty name="tri" property="side3" />
        <br />
        是否能组成三角形:
        <jsp:getProperty name="tri" property="triangle" />
        
        <!-- 如果不先判断是否是triangle的话,servlet类中获得面积时的triangle判断恒为假,area输出结果值始终为-1 -->
        
        面积是:
        <jsp:getProperty name="tri" property="area" />
    </body>
</html>

 


 运行结果:

 

a: b: c: 

三角形的三边是: 边a :2.0 边b :4.0 边c :5.0 
是否能组成三角形: true 面积是: 3.799671038392666

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。