如何在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的`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)

演算法

  • 步驟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中求解斜邊的長度。

更新於:2022年11月17日

2K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.