Java 如何找到八面体的体积

Java 如何找到八面体的体积

八面体是一个具有八个平面面的三维形状。简单地说,它是一个具有八个面、十二条边和六个顶点的多面体。它源自希腊单词”Oktaedron”,意思是”八面体”。

寻找八面体体积的公式如下:

\mathrm{体积: =: \sqrt{2}/3: × :a^3}

其中,’a’代表八面体的边长。

在本文中,我们将看到如何在Java中找到八面体的体积。

展示一些实例

实例1

假设边长为3

然后根据八面体的体积公式计算:

Volume = 12.72

实例2

假设边长为6

根据八面体的体积公式,有−

Volume = 101.82

实例3

假设边长为4.5

根据二十面体的体积公式:

Volume = 42.95

语法

要获取一个数的平方根,我们可以使用Math类的内置 sqrt() 方法,该类位于 java.lang 包中。

以下是使用该方法来获取任意数的平方根的语法:

double squareRoot = Math.sqrt(input_vale)

同样,在Java中要得到一个数的另一个数的幂次方的结果,我们可以使用内置方法 java.lang.Math.pow()

以下是使用该方法得到3的幂次方的语法:

double power = Math.pow(inputValue,3)

步骤

  • 步骤 1 - 通过初始化或用户输入获取八面体的边长。

  • 步骤 2 - 使用体积公式计算八面体的体积。

  • 步骤 3 - 打印结果。

多种方法

我们提供了不同的方法解决问题。

  • 通过使用静态输入值

  • 通过使用用户定义的方法

让我们逐个查看程序及其输出。

方法1:通过使用带有内置方法的静态输入值

在这种方法中,程序中将声明八面体的边长值。然后通过算法找到体积。这里我们将在程序中使用内置的 sqrt()pow() 方法。

示例

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

方法2:使用用户定义的方法

在这个方法中,程序中将声明八面体的边长值。然后调用一个用户定义的方法,将该长度作为参数传递,并在方法内部使用八面体的体积公式来计算体积。

示例

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中找到八面体的体积。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程