如何在 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 包的 Java 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 中求解十二面體的體積。

更新於: 2022 年 10 月 28 日

161 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.