Processing math: 100%

Go语言程序计算球体的体积和表面积


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

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

球体

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


球体的体积

球体占据的空间容量或数量称为其体积。计算球体的体积在我们需要知道足球中可以填充多少空气的情况下非常有用。

=(4/3)π(r)3

球体的表面积

球体占据的总面积称为球体的表面积。计算球体的表面积在我们需要知道给足球表面涂漆的成本的情况下非常有用。

=(4)π(r)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

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

计算球体的体积和表面积

算法

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

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

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

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

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

示例

Open Compiler
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 − 这是我们计算球体体积的函数。该函数具有数据类型为 float64 的变量 'r' 作为参数,并且还具有 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() 并将计算出的值存储在 'volume' 变量中。

  • calculateAreaOfSphere(r float64) float64 − 这是我们计算球体表面积的函数。该函数具有数据类型为 float64 的变量 'r' 作为参数,并且还具有 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 语言编程的信息

更新于: 2022年11月21日

201 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告