首頁 > Java > java教程 > 主體

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

王林
發布: 2023-08-27 13:29:04
轉載
1203 人瀏覽過

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

八面體是具有八個平面的三維形狀。簡單來說,它是一個有八個面、十二邊、六個頂點的多面體。它源自希臘文“Oktaedron”,意思是八面。

八面體體積公式 -

$$\mathrm{體積\: =\: \sqrt{2}/3\: × \:a^3}$$

其中,‘a’指的是八面體的邊長。

在本文中,我們將看到如何在Java中找到八面體的體積。

展示一些實例給你看

Instance-1

的中文翻譯為:

實例-1

假設邊長為3

然後根據八面體的體積公式 −

Volume = 12.72
登入後複製

實例2

假設邊長為6

然後根據八面體的體積公式 −

Volume = 101.82
登入後複製

實例3

假設邊長為4.5

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

Volume = 42.95
登入後複製

文法

要取得一個數字的平方根,我們可以使用 java.lang 套件中 Math 類別的內建 sqrt() 方法。

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

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

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

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

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

演算法

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

  • 步驟 2 - 利用體積公式求出八面體的體積。

  • 第三步 - 列印結果。

多種方法

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

  • 透過使用靜態輸入值

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

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

方法一:使用靜態輸入值與內建方法

在這種方法中,八面體的邊長值將在程式中宣告。然後透過使用演算法找到體積。在此我們將在程式中使用內建的sqrt()pow()方法。

Example

的中文翻譯為:

範例

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

輸出

The side of octahedron: 3.0
Volume of octahedron: 12.727922061357857
登入後複製

方法二:透過使用使用者定義的方法

在這種方法中,八面體的邊長值將在程式中宣告。然後透過將這個長度作為參數呼叫一個使用者定義的方法,並在方法內部使用八面體的體積公式來計算體積。

Example

的中文翻譯為:

範例

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

輸出

The side of octahedron: 10.0
Volume of octahedron: 471.4045207910317
登入後複製

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

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

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