首頁  >  文章  >  Java  >  使用給定的弧的寬度和高度計算圓的半徑的JAVA程序

使用給定的弧的寬度和高度計算圓的半徑的JAVA程序

PHPz
PHPz轉載
2023-08-21 21:21:571262瀏覽

使用給定的弧的寬度和高度計算圓的半徑的JAVA程序

一個圓是一個沒有角的圓形二維圖形。每個圓都有一個起點,圓上的每一個點都與起點保持相等的距離。起點和圓上一點之間的距離稱為圓的半徑。類似地,如果我們從圓的一邊到另一邊畫一條線,並且起點位於中間,那條線被稱為圓的直徑。基本上,直徑是半徑長度的兩倍。

圓弧是指圓的一部份或一段週長。簡單來說,它是圓週上的一個開放曲線。

根據問題陳述,我們需要找到弧的寬度和高度給定時的圓的半徑。

用給定的弧寬和弧高來計算圓的半徑的公式為 −

$$mathrm{r=w^{2}/8h h/2}$$

其中,'r'是一個圓的半徑。

‘h’ 是弧的高度,‘w’ 是弧的寬度。

所以,讓我們繼續看看如何使用Java程式語言來找到具有給定弧寬和弧高的圓的半徑。

展示一些實例給你看−

Instance-1

的中文翻譯為:

實例-1

Let say height (h) of arc = 2.
And width of (w) of arc = 4
Radius of circle by using given width and height of arc = 2

Instance-2

的中文翻譯為:

實例-2

Let say height (h) of arc = 3.
And width of (w) of arc = 5
Radius of circle by using given width and height of arc = 2.54

Instance-3

的中文翻譯為:

實例-3

Let say height (h) of arc = 1.
And width of (w) of arc = 4.
Radius of circle by using given width and height of arc = 2.5.

文法

要在Java中取得一個數的任意次冪,我們可以使用內建的 java.lang.Math.pow() 方法。

以下是使用方法來取得2的冪的語法 -

double power = Math.pow (inputValue,2)

演算法

步驟-1 − 透過靜態輸入或使用者輸入取得弧的寬度和高度。

步驟-2 − 透過使用公式找到圓的半徑。

第三步驟 - 列印結果。

多種方法

我們以不同的方法提供了解決方案。

  • 透過使用靜態輸入值。

  • 透過使用使用者定義的方法。

讓我們逐一查看程式及其輸出。

方法一:透過使用靜態輸入值

在這個方法中,我們宣告兩個雙精確度變數來保存弧的寬度和高度值,並在程式中初始化這些值。然後透過使用演算法,我們可以找到圓的半徑。

Example

的中文翻譯為:

範例

public class Main {
   //main method
   public static void main(String[] args) {
      //width (w) and height (h) of arc
      double w = 5, h = 3;
      //find radius using the formula
      double r = ((w * w) / (8 * h) + h / 2);
      System.out.println( "Radius of the circle: "+r);
   }
 }

輸出

Radius of the circle: 2.541666666666667

方法二:使用靜態輸入值的使用者定義方法。

在這個方法中,我們宣告兩個雙精確度變數來保存弧的寬度和高度值,並將這些值作為使用者輸入。然後透過將這些值作為參數傳遞來呼叫一個使用者定義的方法。然後在方法內部,透過使用演算法,我們可以找到圓的半徑。

Example

的中文翻譯為:

範例

public class Main {
   //main method
   public static void main(String[] args) {
      //width (w) and height (h) of arc
      double w = 5, h = 2;
      //call the user defined method to get the radius
      findRadius(w, h);
   }

   //find radius of circle
   static void findRadius(double w, double h) {
      //find radius using the formula
      double r = ((w * w) / (8 * h) + h / 2);
      System.out.println( "Radius of the circle: "+r);
   }
}

輸出

Radius of the circle: 2.5625

在本文中,我們探討如何使用不同的方法在Java中找到圓的半徑,當圓的弧的寬度和高度已知時。

以上是使用給定的弧的寬度和高度計算圓的半徑的JAVA程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除