首頁 > Java > java教程 > 主體

如何在Java中找到十二面體的體積?

王林
發布: 2023-09-05 10:13:09
轉載
780 人瀏覽過

如何在Java中找到十二面體的體積?

十二面體是一種具有十二個平面的三維形狀。它源自於兩個希臘詞,即“dodeka”,意思是“12”和“hedra”,意思是“臉”。簡單地說,它是一個有十二條邊或面的多面體。它也被稱為十二面體。

求十二面體體積的公式 -

$$\mathrm{體積\:=\: (15\: \: 7\sqrt{5})*a^3/4}$$

其中,「a」指的是十二面體的邊緣。

在本文中,我們將了解如何在 Java 中求出十二面體的體積。

向您展示一些實例

實例1

假設邊長為4

然後根據十二面體的體積公式 -

Volume = 490.44
登入後複製

實例2

假設邊長為3

然後根據十二面體的體積公式 -

Volume = 206.904
登入後複製

實例3

假設邊長為4.2

然後根據十二面體的體積公式 -

Volume = 567.745
登入後複製

文法

為了取得數字的平方根,我們在 java.lang 套件的 Math 類別中內建了 sqrt() 方法。

以下是使用此方法取得任意數字的平方根的語法。

double squareRoot = Math.sqrt(input_vale)
登入後複製

類似地,為了在 Java 中獲得任何數字的冪到另一個數字的冪,我們內建了 java.lang.Math.pow() 方法。

以下是使用此方法取得 3 次方的語法

double power = Math.pow (inputValue,3)
登入後複製

演算法

  • 步驟 1 - 透過初始化或使用者輸入來取得十二面體的邊長。

  • 步驟 2 - 使用體積公式計算十二面體的體積

  • #第 3 步 - 列印結果。

多種方法

我們透過不同的方式提供了解決方案。

  • 透過使用使用者輸入值

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

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

方法 1:使用靜態輸入值

在這種方法中,十二面體的邊長將在程式中宣告。然後使用演算法求出體積。

範例

import java.util.*;
public class Main{
   //main method
   public static void main(String args[]){
      //declared the edge length
      double a=5.5;
      System.out.println("Enter the length of edge:"+a);
      //Find volume by using formula
      double volume= (((15 + (7 * (Math.sqrt(5)))) / 4)
      * (Math.pow(a, 3)));
      //Print the result
      System.out.println("Volume of Dodecahedron: " +volume);
   }
}
登入後複製

輸出

Enter the length of edge:5.5
Volume of Dodecahedron: 1274.9514170739233
登入後複製

方法2:使用使用者定義的方法

在此方法中,將要求使用者輸入十二面體的邊長。然後將此長度作為參數傳遞來呼叫使用者定義的方法,並在方法內部使用十二面體的體積公式求出體積。

範例

import java.util.*;
public class Main{
   //main method
   public static void main(String args[]){
      //declared the edge length
      double a=6;
      System.out.println("The length of edge: "+a);
      //calling the method
      findVolume(a);
   }
   //user defined method to find volume of dodecahedron
   public static void findVolume(double a){
      //Find volume by using formula
      double volume= (((15 + (7 * (Math.sqrt(5)))) / 4)
      * (Math.pow(a, 3)));
      //Print the result
      System.out.println("Volume of Dodecahedron: " +volume);
   }
}
登入後複製

輸出

The length of edge: 6.0
Volume of Dodecahedron: 1655.2336954949205
登入後複製

在本文中,我們探討如何使用不同的方法在 Java 中求出十二面體的體積。

以上是如何在Java中找到十二面體的體積?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!