码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-11-05 09:11:13      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   java   for   

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

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

标签:style   blog   http   io   color   ar   os   java   for   

原文地址:http://www.cnblogs.com/balfish/p/4075338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!