首頁  >  文章  >  Java  >  如何在Java中找到斜邊的長度?

如何在Java中找到斜邊的長度?

PHPz
PHPz轉載
2023-09-09 22:33:05567瀏覽

如何在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刪除