Java 如何找到斜边的长度

Java 如何找到斜边的长度

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

可以使用勾股定理来找到斜边的长度。根据勾股定理,两条直角边的平方和等于斜边的平方,即

a2+ b2 = c2

在这里,a、b和c分别指的是直角三角形的三条边。

So, Hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2))

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

为了向您展示一些实例

实例1

假设底边和高度分别为3和4。

然后通过使用毕达哥拉斯定理:

Length of hypotenuse is 5

实例2

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

然后使用勾股定理公式

Length of hypotenuse is 12.8

实例3

假设底边长为6.5,高为8。

那么通过使用勾股定理公式:

Length of hypotenuse is 10.3

语法

要获取一个数字的平方根,我们可以使用Java语言库的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)

步骤

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

  • 步骤2 - 使用公式找到斜边的长度。

  • 步骤3 - 打印结果。

多种方法

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

  • 通过使用勾股定理

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

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

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

方法1:通过静态输入

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

示例

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

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

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

示例

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:使用用户定义的方法

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

示例

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中找到斜边的长度。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程