用java编写程序求三角形的面积

Python014

用java编写程序求三角形的面积,第1张

构成三角形的条件,要根据任意两边和要大于第三边。代码如下:

import java.util.Scanner

public class woo {

public static void main(String args[]) {

Scanner scan =new Scanner(System.in)

System.out.println("输入三角形的三边")

int a = scan.nextByte()

int b = scan.nextByte()

int c = scan.nextByte()

float s =(a+b+c)/2f

float S = (float) Math.sqrt(s*(s-a)*(s-b)*(s-c))

if (a+b>c &&b+c>a &&a+c>b){

System.out.println(S)

}

else{

System.out.println("不构成三角形")

}

}

}

扩展资料

三角形是由同一平面内不在同一直线上的三条线段‘首尾’顺次连接所组成的封闭图形,在数学、建筑学有应用。

常见的三角形按边分有普通三角形(三条边都不相等),等腰三角(腰与底不等的等腰三角形、腰与底相等的等腰三角形即等边三角形);按角分有直角三角形、锐角三角形、钝角三角形等,其中锐角三角形和钝角三角形统称斜三角形。

三角形在平面上三角形的内角和等于180°,在平面上三角形的外角和等于360° , 在平面上三角形的外角等于与其不相邻的两个内角之和。

参考资料:

三角形面积公式——百度百科

public class Triangle {

    private Integer bottom

    private Integer height

    public Triangle(Integer bottom, Integer height) {

        this.bottom = bottom

        this.height = height

    }

    public Integer getBottom() {

        return bottom

    }

    public void setBottom(Integer bottom) {

        this.bottom = bottom

    }

    public Integer area() {

        return this.bottom * this.height / 2

    }

} public class TriangleTest {

    public static void main(String[] args) {

        Triangle triangle = new Triangle(10, 5)

        System.out.println(triangle.area())

    }

}

如果觉得可以可以关注简书/微信公众号:早晚程序员