📜  求三角形面积的Java程序

📅  最后修改于: 2022-05-13 01:55:01.366000             🧑  作者: Mango

求三角形面积的Java程序

三角形是多边形。它有三个边和三个顶点,每个顶点都来自一个角度。它是一个封闭的二维形状。在本文中我们将学习如何求三角形的面积。

根据情况计算三角形面积时可能有两种可能性

  • 使用三角形的高和底
  • 使用三角形的3条边

情况1:当给定三角形的高和底时,则三角形的面积底和高乘积的一半。

公式:

示例 1:使用底和高评估面积

Java
// Java program to find the
// area of the triangle
  
// Importing java libraries
import java.io.*;
  
class GFG {
  
    // Function to calculate the
    // area of the triangle
    static double area(double h, double b)
    {
        // Function returning the value that is
        // area of a triangle
        return (h * b) / 2;
    }
  
    // Main driver code
    public static void main(String[] args)
    {
        // Custom inputs- height and base values
  
        // Height of the triangle
        double h = 10;
  
        // Base of the triangle
        double b = 5;
  
        // Calling area function and
        // printing value corresponding area
        System.out.println("Area of the triangle: "
                           + area(h, b));
    }
}


Java
// Java program to find the area of
// the triangle using Heron’s formula
  
// Importing java libraries
import java.io.*;
  
class GFG {
  
    // Function to calculate the area where parameters
    // passed are three sides of a triangle
    static float area(float r, float s, float t)
    {
        // Condition check over sides of triangle
        if (r < 0 || s < 0 || t < 0 || (r + s <= t)
            || r + t <= s || s + t <= r)
  
        // Length of sides must be positive and sum of
        // any two sides must be smaller than third side
  
        {
            // print message if condition fails
            System.out.println("Not a valid input");
            System.exit(0);
        }
  
        /*else*/
  
        // Finding Semi perimeter of the triangle
        // using formula
        float S = (r + s + t) / 2;
  
        // Finding the area of the triangle
        float A = (float)Math.sqrt(S * (S - r) * (S - s)
                                   * (S - t));
  
        // return area value
        return A;
    }
  
    // Main driver code
    public static void main(String[] args)
    {
        // custom inputs of sides of values
  
        // Sides of the triangle
        float r = 5.0f;
        float s = 6.0f;
        float t = 7.0f;
  
        // Calling area function and
        // printing the area of triangle
        System.out.println("Area of the triangle: "
                           + area(r, s, t));
    }
}


输出:

Area of the triangle: 25.0

情况 2:当三角形的三边都给定时

现在假设如果我们只知道边,则不能应用上述公式。面积将使用三角形的尺寸计算。这个公式通常被称为 海伦公式。

算法:

  1. 计算三角形的半周长。
  2. 半米与 3 个值的乘积,其中这些其余值是计算出的半周长以上的边差。
  3. 从计算中获得的上述值的平方根给出三角形的面积。

示例 2:

Java

// Java program to find the area of
// the triangle using Heron’s formula
  
// Importing java libraries
import java.io.*;
  
class GFG {
  
    // Function to calculate the area where parameters
    // passed are three sides of a triangle
    static float area(float r, float s, float t)
    {
        // Condition check over sides of triangle
        if (r < 0 || s < 0 || t < 0 || (r + s <= t)
            || r + t <= s || s + t <= r)
  
        // Length of sides must be positive and sum of
        // any two sides must be smaller than third side
  
        {
            // print message if condition fails
            System.out.println("Not a valid input");
            System.exit(0);
        }
  
        /*else*/
  
        // Finding Semi perimeter of the triangle
        // using formula
        float S = (r + s + t) / 2;
  
        // Finding the area of the triangle
        float A = (float)Math.sqrt(S * (S - r) * (S - s)
                                   * (S - t));
  
        // return area value
        return A;
    }
  
    // Main driver code
    public static void main(String[] args)
    {
        // custom inputs of sides of values
  
        // Sides of the triangle
        float r = 5.0f;
        float s = 6.0f;
        float t = 7.0f;
  
        // Calling area function and
        // printing the area of triangle
        System.out.println("Area of the triangle: "
                           + area(r, s, t));
    }
}

输出:

Area of the triangle: 14.6969385