已知弧的寬和高,計算圓的半徑的 JAVA 程式


圓是一個沒有角的二維圓形圖形。每個圓都有一個原點,圓上的每個點到原點的距離都相等。原點和圓上一點之間的距離稱為圓的半徑。同樣,如果我們從圓的一端到另一端畫一條線,並且原點位於這條線的中點,那麼這條線稱為圓的直徑。基本上,直徑是半徑長度的兩倍。

圓弧指的是圓周的一部分或一部分。簡單來說,它是圓周上的一條開放曲線。

根據題意,我們必須在給出弧的寬度和高度的情況下找到圓的半徑。

已知弧的寬度和高度,求圓半徑的公式:

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

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

'h' 是弧的高度,'w' 是弧的寬度。

因此,讓我們來看看如何使用 Java 程式語言根據給定的弧的寬度和高度來找到圓的半徑。

舉幾個例子:

示例 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

示例 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

示例 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** - 使用公式求圓的半徑。

**步驟 3** - 列印結果。

多種方法

我們提供了不同的方法來解決這個問題。

  • 使用靜態輸入值。

  • 使用使用者自定義方法。

讓我們逐一檢視程式及其輸出。

方法 1:使用靜態輸入值

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

示例

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

方法 2:使用帶有靜態輸入值的使用者定義方法。

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

示例

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 中的不同方法來找到已知圓弧寬度和高度的圓的半徑。

更新於:2022年12月27日

2K+ 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.