首页  >  文章  >  Java  >  如何在Java中找到八面体的体积?

如何在Java中找到八面体的体积?

王林
王林转载
2023-08-27 13:29:041182浏览

如何在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删除