首页> Java> java教程> 正文

如何在Java中找到斜边的长度?

PHPz
发布: 2023-09-09 22:33:05
转载
545 人浏览过

如何在Java中找到斜边的长度?

斜边是指直角三角形中与直角相对的最长边。

可以使用毕达哥拉斯定理找到斜边的长度。根据毕达哥拉斯定理,两条边长度的平方和等于第三条边长度的平方,即

a2+ b2 = c2
登录后复制

其中,a、b、c表示直角三角形的三条边。

So, Hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2))
登录后复制

在本文中,我们将看到如何使用Java编程语言找到斜边的长度。

展示给你一些实例

Instance-1

的中文翻译为:

实例-1

假设底长和高分别为 3 和 4。

然后通过使用勾股定理公式,

Length of hypotenuse is 5
登录后复制

Instance-2

的中文翻译为:

实例-2

假设底边长和高分别为8和10

然后通过使用勾股定理公式,

Length of hypotenuse is 12.8
登录后复制

Instance-3

的中文翻译为:

实例-3

假设底长和高分别为 6.5 和 8。

然后通过使用勾股定理公式,

Length of hypotenuse is 10.3
登录后复制

语法

要得到一个数字的平方根,我们可以使用java.lang包中的Math类中内置的sqrt()方法。

以下是使用该方法获取任意数字的平方根的语法

double squareRoot = Math.sqrt(input_vale)
登录后复制

类似地,为了在 Java 中获得任何数字的幂到另一个数字的幂,我们内置了java.lang.Math.pow()方法。

以下是使用该方法获取2的幂的语法。

double power = Math.pow(inputValue,2)
登录后复制

java.lang 包的 Math 类有一个内置方法Math.hypot(),它返回其参数平方和的平方根。

为了得到斜边的长度,我们使用以下公式

Hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2))
登录后复制

通过使用Math.hypot()函数,我们可以得到斜边的长度。

Hypotenuse = Math.hypot(base, height)
登录后复制

算法

  • 第一步- 通过初始化或用户输入获取三角形的另外两边的长度,即高度和底边。

  • 步骤 2- 使用公式计算斜边的长度。

  • 第 3 步- 打印结果。

多种方法

我们通过不同的方式提供了解决方案。

  • 通过使用毕达哥拉斯定理

  • 通过使用内置 Math.hypot() 函数

  • 通过使用用户定义的方法

让我们一一看看该程序及其输出。

方法 1:使用静态输入

在这种方法中,三角形的高度和底边长度将在程序中初始化。然后通过使用勾股定理公式,找到斜边的长度。

Example

的中文翻译为:

示例

import java.util.*; public class Main { public static void main(String[] args) { //initialized length of the base double base = 10; System.out.println("Given length of base: "+base); //initialized length of the height double height = 20; System.out.println("Given length of height: "+height); //find the length of hypotenuse by using Pythagoras formula double hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2)); //print the result System.out.println("Length of the hypotenuse: " + hypotenuse); } }
登录后复制

输出

Given length of base: 10.0 Given length of height: 20.0 Length of the hypotenuse: 22.360679774997898
登录后复制

方法二:通过使用用户输入的值

在这种方法中,三角形的高和底的长度将在程序中初始化。然后使用内置的hypot()函数,找到斜边长度。

Example

的中文翻译为:

示例

import java.util.*; public class Main { public static void main(String[] args) { //initialized length of the base double base = 15; System.out.println("Given length of base: "+base); //initialized length of the height double height = 20; System.out.println("Given length of height: "+height); //find the length of hypotenuse by using Math.hypot() method double hypotenuse = Math.hypot(base, height); //print the result System.out.println("Length of the hypotenuse: " + hypotenuse); } }
登录后复制

输出

Given length of base: 15.0 Given length of height: 20.0 Length of the hypotenuse: 25.0
登录后复制

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

在这种方法中,三角形的高度和底边的长度将在程序中进行初始化。然后通过将这些值作为参数传递并在方法内部调用用户定义的方法来找到斜边的长度。

Example

的中文翻译为:

示例

import java.util.*; public class Main { public static void main(String[] args) { //initialized length of the base double base = 12; System.out.println("Given length of base: "+base); //initialized length of the height double height = 18; System.out.println("Given length of height: "+height); //calling the user defined method hypotenuseLength(base,height); } //find length of hypotenuse public static void hypotenuseLength(double base, double height) { //find the length of hypotenuse by using Math.hypot() method double hypotenuse = Math.hypot(base, height); //print the result System.out.println("Length of the hypotenuse: " + hypotenuse); } }
登录后复制

输出

Given length of base: 12.0 Given length of height: 18.0 Length of the hypotenuse: 21.633307652783937
登录后复制

在本文中,我们探讨了如何通过使用不同的方法在Java中找到斜边的长度。

以上是如何在Java中找到斜边的长度?的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!