如何在Java中計算九邊形的面積?


九邊形是指一個具有9條邊和9個內角的多邊形。當多邊形的邊長都相等時,它被稱為正九邊形,其內角為140度,所有內角之和為1260度。九邊形也稱為九角形。

九邊形的面積可以透過以下方法計算:

已知邊長(s)

Area = 6.1818 * (s * s)

已知外接圓半徑(r)

Area = 2.8925 * (r * r)

已知旁心距(a)

Area = 3.2757 * (a * a)

在本文中,我們將學習如何使用Java程式語言計算九邊形的面積。

舉幾個例子

例1

邊長(s) = 4.5

使用已知邊長的九邊形面積公式,

Area = 125.18

例2

外接圓半徑(r) = 5

使用已知外接圓半徑的九邊形面積公式,

Area = 72.31

例3

旁心距(a) = 6

使用已知旁心距的九邊形面積公式,

Area = 117.92

語法

為了在Java中獲取任何數的冪,我們有內建的java.lang.Math.pow()方法。

以下是使用該方法獲取2的冪的語法:

double power = Math.pow (inputValue,2)

演算法

  • 步驟1 − 透過初始化或使用者輸入獲取九邊形的邊長、外接圓半徑或旁心距。

  • 步驟2 − 然後使用九邊形的相應面積公式計算面積。

  • 步驟3 − 列印九邊形的面積值。

多種方法

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

  • 使用邊長(s)

  • 使用外接圓半徑(r)

  • 使用旁心距(a)

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

方法1:使用邊長(s)

在這種方法中,宣告並初始化九邊形的邊長值。然後使用演算法求面積。

示例

import java.util.*; public class Main { //main method public static void main(String[] args) { //side length of enneagon double s = 5.5; //find area by using formula double area = 6.1818 * (s * s); //print the result System.out.println("Area of enneagon is: " + area); } }

輸出

Area of enneagon is: 186.99945

方法2:使用外接圓半徑(r)

在這種方法中,宣告並初始化九邊形的外接圓半徑。然後使用演算法求面積。

示例

import java.util.*; public class Main { //main method public static void main(String[] args) { //circumradius of enneagon double r = 4.5; //find area by using formula double area = 2.8925 * (r * r); //print the result System.out.println("Area of enneagon is: " + area); } }

輸出

Area of enneagon is: 58.573125000000005

方法3:使用旁心距(a)

在這種方法中,宣告並初始化九邊形的旁心距。然後使用演算法求面積。

示例

import java.util.*; public class Main { //main method public static void main(String[] args) { //apothem of enneagon double a = 6; //find area by using formula double area = 3.2757 * (a * a); //print the result System.out.println("Area of enneagon is: " + area); } }

輸出

Area of enneagon is: 117.9252

在本文中,我們探討了如何使用不同的方法在Java中計算九邊形的面積。

更新於:2022年11月17日

125 次瀏覽

開啟你的職業生涯

完成課程獲得認證

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