Golang 计算球体的体积和表面积

Golang 计算球体的体积和表面积

在本教程中,我们将讨论使用球体的半径来计算球体的体积和表面积的方法,使用Go语言编程。

但是,在编写此代码之前,让我们简要讨论一下球体及其体积和表面积。

球体

球体是一个三维图形,其所有点都与中心等距离。它没有边和面。球体的半径用 ‘r’ 表示。球是球体的一个很好的示例。

Golang 计算球体的体积和表面积

球体的体积

球体所占据的容量或空间量被称为其体积。在我们想知道我们可以在一个足球中装多少空气时,计算球体的体积是有益的。

\mathrm{体积 \, =\, \left ( 4/3 \right )\ast \pi \ast \left ( r \right )^{3} }

球体的表面积

由球体占据的总面积被称为球体的表面积。在我们想知道给一个足球涂漆的费用时计算球体的表面积是有益的。

\mathrm{表面积 \, =\, \left ( 4 \right )\ast \pi \ast \left ( r \right )^{2}}

示例

  • 半径 = 5

球体的体积 = 523.8095238095239

球体的表面积 = 314.2857142857143

解释

球体的体积 = (4/3) * 𝛑 * (r) 3

= (4/3) * (22/7) * (5*5)

= 523.8095238095239

球体的表面积 = 4 * 𝛑 * (r) 2

= 4 * (22/7) * 5 * 5

= 314.2857142857143

  • 半径 = 30

球体的体积 = 113142.85714285714

球体的表面积 = 11314.285714285714

计算球体的体积和表面积

步骤

步骤1 − 声明一个用于存储球体半径的变量 ‘r’。

步骤2 − 声明两个变量用于存储球体的表面积 ‘area’ 和体积 ‘volume’,并将两个变量初始化为0。

步骤3 − 使用公式计算体积- 体积 = (4/3)* 𝛑 *(r) 3 ,并将其存储在calculateVolumeOfSphere()函数的’volume’变量中。

步骤4 − 使用公式计算面积- 面积 = 4 * 𝛑 *(r) 2 ,并将其存储在calculateAreaOfSphere()函数的’area’变量中。

步骤5 − 打印出计算得到的球体体积和面积,即存储在’volume’和’area’变量中的值。

示例

package main

// fmt package allows us to print formatted strings
import "fmt"
func calculateVolumeOfSphere(r float64) float64 {

   // declaring variable ‘volume’ for storing the volume of the sphere
   var volume float64 = 0

   // calculating the volume of the sphere using its formula
   volume = (4.0 / 3.0) * (22.0 / 7.0) * r * r * r
   return volume
}
func calculateAreaOfSphere(r float64) float64 {

   // declaring variable ‘area’ for storing the area of the sphere
   var area float64 = 0

   // calculating the area of the sphere using its formula
   area = 4 * (22.0 / 7.0) * r * r
   return area
}
func main() {

   // declaring variable ‘radius’ for storing the radius of the sphere
   var r float64 = 5

   // declaring variables ‘area’ and ‘volume’ for storing the area and volume of the sphere
   var area, volume float64
   fmt.Println("Program to calculate the volume and area of the Sphere \n")

   // calling function calculateVolumeOfSphere() for
   // calculating the volume of the sphere
   volume = calculateVolumeOfSphere(r)

   // calling function calculateAreaOfSphere() for calculating
   // the area of the sphere
   area = calculateAreaOfSphere(r)

   // printing the results
   fmt.Println("Radius of the sphere : ", r)
   fmt.Println("Therefore, Volume of the sphere : ", volume)
   fmt.Println("Area of the sphere : ", area)
}

输出

Program to calculate the volume and area of the Sphere 

Radius of the sphere :  5
Therefore, Volume of the sphere :  523.8095238095239
Area of the sphere :  314.2857142857143

代码说明

  • var r float64 = 5 − 在这行中,我们声明了变量‘r’用于存储半径。由于体积和面积的数据类型都是float,所以我们使用了float64数据类型。

  • calculateVolumeOfSphere(r float64) float64 − 这是一个函数,用于计算球体的体积。函数有一个参数‘r’,数据类型为float64,也有一个返回类型为float64。

  • volume = (4.0 / 3.0) * (22.0 / 7.0) * r * r * r − 如果我们希望输出的数据类型为float,我们需要显式地转换数值,这就是为什么我们使用4.0和3.0而不是4和3的原因。使用上述公式,我们可以计算出球体的体积。

  • return volume − 返回球体的体积。

  • volume = calculateVolumeOfSphere(r) − 我们调用calculateVolumeOfSphere()函数,并将计算出的值存储在main方法中的‘volume’变量中。

  • calculateAreaOfSphere(r float64) float64 − 这是一个函数,用于计算球体的表面积。函数有一个参数‘r’,数据类型为float64,也有一个返回类型为float64。

  • area = 4 * (22.0 / 7.0) * r * r − 如果我们希望输出的数据类型为float,我们需要显式地转换数值,这就是为什么我们使用22.0和7.0而不是22和7的原因。使用这个公式,我们计算出了球体的表面积。

  • return area − 返回球体的表面积。

  • area = calculateAreaOfSphere(r) − 我们调用calculateAreaOfSphere()函数,并将计算出的值存储在‘area’变量中。

  • fmt.Println(“因此,球体的体积为:”, volume) − 和 fmt.Println(“球体的表面积为:”, area) − 用于打印结果。

结论

这就是使用Go编程计算球体的体积和表面积的全部内容。我们还通过使用分开的函数来计算面积和体积来保持代码的模块化,这也增加了代码的可重用性。您可以通过使用 这些 教程进一步了解Go编程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程