Java 如何找到给定底边和面积的三角形的最小高度

Java 如何找到给定底边和面积的三角形的最小高度

我们有三角形的面积’a’和底边’b’。根据问题陈述,我们需要使用Java编程语言来找到最小高度’h’。

我们知道当给定底边和高度时,三角形的面积为 –

\mathrm{area :=: \frac{1}{2}: * :base: *: height}

使用上述公式,我们可以从中获得高度 –

height = (2 * area) / base

然后使用内置的 ceil() 方法我们可以得到最小高度。

为了向您展示一些实例

实例1

假设给定区域= 12和底部= 6

然后通过使用公式来获取高度,

最小高度= 4.0

实例2

假设给定区域= 8和底部= 4

然后通过使用公式来获取高度,

最小高度= 4.0

实例3

假设给定区域= 12和底部= 5

然后通过使用公式来获取高度,

最小高度= 5.0

语法

在Java中,我们有 Math.ceil() 方法,用于获得最接近给定浮点数的数学整数(即最小整数),该数大于或等于给定浮点数。

以下是语法。

Math.ceil(double value);

步骤

  • 步骤1 - 通过初始化或用户输入获得三角形的面积和底边值。

  • 步骤2 - 使用公式计算高度。

  • 步骤3 - 使用Math.ceil()方法找到最小高度。

  • 步骤4 - 打印结果。

多种方法

我们提供了不同的解决方法。

  • 使用静态输入值

  • 使用用户定义的方法

让我们逐个查看程序及其输出。

方法1:使用静态输入值

在这种方法中,三角形的底边和面积值将在程序中声明,然后使用算法找到三角形的最小高度。

示例

import java.util.*;
import java.io.*;
public class Main {
   //main method
   public static void main(String args[]){

      //Declared the area of triangle
      double area = 6;
      System.out.println("Area of triangle: "+area);

      //Declared the base of triangle
      double base = 14;
      System.out.println("Base of triangle: "+base);

      //Find height of triangle
      double height = (2 * area) / base;
      System.out.println("Height: " + height);

      //Find minimum height of triangle by using ceil() method
      double minHeight = Math.ceil(height);
      System.out.println("Minimum height: " + minHeight);
   }
}

输出

Area of triangle: 6.0
Base of triangle: 14.0
Height: 0.8571428571428571
Minimum height: 1.0

方法2:使用用户定义的方法

在这个方法中,三角形的底和面积的值将在程序中声明。然后通过将这个底和面积作为参数调用一个用户定义的方法。

在方法内部,通过使用公式计算三角形的最小高度。

示例

import java.util.*;
import java.io.*;
public class Main{
   //main method
   public static void main(String args[]){

      //Declared the area of triangle
      double area = 12;
      System.out.println("Area of triangle: "+area);

      //Declared the base of triangle
      double base = 6;
      System.out.println("Base of triangle: "+base);

      //calling a user defined method
      findHeight(area,base);
   }
   //user defined method
   public static void findHeight(double area, double base){

      //Find height of triangle
      double height = (2 * area) / base;
      System.out.println("Height: " + height);

      //Find minimum height of triangle by using ceil() method
      double minHeight = Math.ceil(height);
      System.out.println("Minimum height: " + minHeight);
   }
}

输出

Area of triangle: 12.0
Base of triangle: 6.0
Height: 4.0
Minimum height: 4.0

在这篇文章中,我们探讨了如何通过使用不同的方法,在给定底边和面积的情况下,计算三角形的最小高度(用Java实现)。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程