Golang 计算立方体体积
在本教程中,我们将讨论使用Golang编程程序计算立方体体积的方法,使用立方体的边长。
但在编写此代码之前,让我们简要讨论立方体及其体积。
立方体
立方体是一个有六个正方形面的三维图形。立方体的六个面都是正方形。其长度、宽度和高度相等。骰子是一个常见的立方体的示例。
立方体的体积
立方体占据的总三维空间被称为立方体的体积。在我们想要知道一个立方体物体的容量的情况下,计算立方体的体积是有益的。
\mathrm{长度\, = \, 宽度 \, =\, 高度}
公式
立方体的体积可以通过将边长乘以三次来计算。因此,公式可以写为
\mathrm{体积 \, =\, 边长 * 边长 * 边长}
举例
- 边长 = 6
立方体的体积 = 6 * 6 * 6 = 216
由于体积是通过将边长乘以三次计算得出的,因此我们将边长-‘6’乘以三次,即6 * 6 * 6,结果为216,即立方体的体积。
- 边长 = 12.5
立方体的体积 = 12.5 * 12.5 * 12.5 = 1953.125
将边长-‘12.5’乘以三次,即12.5 * 12.5 * 12.5,结果为1953.125,即立方体的体积。
在函数中查找立方体的体积
步骤
步骤1 - 声明一个变量用于存储立方体的边长-‘side’。
步骤2 - 声明一个变量用于存储立方体的体积-‘volume’,并初始化为0。
步骤3 - 在主函数中通过将边长乘以三次来计算体积,并将其存储在变量‘volume’中。
步骤4 - 打印计算得到的体积,即变量‘volume’中存储的值。
举例
package main
// fmt package allows us to print formatted strings
import "fmt"
func main() {
fmt.Println("Program to find the volume of a cube \n")
// declaring variable ‘side’ for storing length of cube
var side float64 = 6
// declaring variable ‘volume’ for storing the volume of cube
var volume float64 = 0
// calculating the volume of the cube
volume = side * side * side
// printing the results
fmt.Println("Dimension of the side : ", side)
fmt.Println("Therefore, Volume of cube : ", volume)
}
输出
Program to find the volume of a cube
Dimension of the side : 6
Therefore, Volume of cube : 216
代码描述
- var side float64 = 5, var volume float64 = 0 − 在此行中,我们声明了side和volume变量。由于side和volume的数据类型为float,因此我们使用了float64数据类型。
-
volume = side * side * side − 我们使用公式volume = side * side * side来计算立方体的体积。
-
fmt.Println(“Side的尺寸:”, side) − 打印立方体的边长。
-
fmt.Println(“因此,立方体的体积:”, volume) − 打印计算出的立方体的体积。
因此,volume = 6 * 6 * 6
volume = 216
使用不同函数计算立方体的体积
步骤
步骤1 − 声明一个变量来存储立方体的边长- ‘side’。
步骤2 − 声明一个变量来存储立方体的体积- ‘volume’,并将其初始化为0。
步骤3 − 通过将边长乘以三次来计算体积,并将其存储在calculateVolumeOfCube()函数中的’volume’变量中。
步骤4 − 通过在main()函数中调用calculateVolumeOfCube()函数来打印计算出的体积,即存储在’volume’变量中的值。
示例
package main
// fmt package allows us to print formatted strings
import "fmt"
func calculateVolumeOfCube(side float64) float64 {
// declaring variable ‘volume’ for storing the volume of cube
var volume float64 = 0
// calculating the volume of the cube
volume = side * side * side
return volume
}
func main() {
// declaring variable ‘side’ for storing length of cube
var side float64 = 15
var volume float64
fmt.Println("Program to find the volume of a cube \n")
// calling function calculateVolumeOfCube() for calculating
// the volume of the cube
volume = calculateVolumeOfCube(side)
// printing the results
fmt.Println("Dimension of the side : ", side)
fmt.Println("Therefore, Volume of cube : ", volume)
}
输出
Program to find the volume of a cube
Dimension of the side : 15
Therefore, Volume of cube : 3375
代码描述
- var side float64 = 5, var volume float64 − 在这一行中,我们声明了变量side和volume。由于side和volume的数据类型都是float64,所以我们使用了float64数据类型。
-
calculateVolumeOfCube(side float64) float64 − 这是计算立方体体积的函数。该函数有一个名为’side’的形参,数据类型为float64,返回类型也是float64。
-
volume = side * side * side − 我们使用这个公式side * side * side来计算立方体的体积。
-
return volume − 返回计算出的立方体体积。
-
volume = calculateVolumeOfCube(side) − 我们调用calculateVolumeOfCube()函数,并将计算出的值存储在变量’volume’中。
-
fmt.Println(“Dimension of the side : “, side) − 打印立方体的边长。
-
fmt.Println(“Therefore, Volume of cube : “, volume) − 打印计算出的立方体体积。
因此,volume = 15 * 15 * 15
volume = 3375
结论
这就是使用两种方法计算立方体体积的全部内容。第二种方法在代码可重用性和模块化方面更好,我们可以在任何地方多次调用该函数,并传递不同的值。您可以使用 这些 教程进一步探索Golang编程。