Golang 计算一个立方体的表面积

Golang 计算一个立方体的表面积

在本教程中,我们将讨论使用立方体的边来计算Golang编程中立方体表面积的方法。

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

立方体

立方体是一个有六个正方形面的三维图形。立方体的所有六个面都是正方形的。其长度、宽度和高度相等。骰子是立方体的一个常见示例。

立方体的面积

由立方体的所有六个面所围成的总面积被称为立方体的表面积。计算立方体的面积在想要给立方体表面涂漆或包裹一张纸时会有益。

Golang 计算一个立方体的表面积

公式

立方体的表面积定义为立方体所有侧面的面积之和。因此,公式可以写为六倍边长的平方。

\mathrm{Area\, =\, 6\ast \left ( side \right )^{2}}

示例

  • 边长 = 5

立方体的面积 = 6 * (5)2 = 150

由于面积是通过将边的平方乘以6计算得出的,因此我们将6和(5)2相乘,即6 * 25,这结果为立方体的面积为150。

  • 边长 = 10.5

立方体的面积 = 6 * (10.50)2 = 661.50

将6和(10.50)2相乘,即6 * 110.25,这结果为立方体的面积为661.50。

在主函数中计算立方体的面积

步骤

步骤1 - 声明一个变量用于存储立方体的边长- ‘side’。

步骤2 - 声明一个变量用于存储立方体的面积- ‘area’,并将其初始化为0。

步骤3 - 通过将边长的平方乘以6计算面积,并将其存储在函数内的’area’变量中。

步骤4 - 打印计算得到的面积,即存储在变量’area’中的值。

示例

package main
// fmt package allows us to print formatted strings
// math package allows us to perform various mathematical
// operations
import (
   "fmt"
   "math"
)
func main() {
   fmt.Println("Program to find the area of a cube \n")

   // declaring variable ‘side’ for storing the length of the cube
   var side float64 = 5

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

   // calculating the area of the cube
   area = 6 * math.Pow(side, 2)

   // printing the results
   fmt.Println("Dimension of the side : ", side)
   fmt.Println("Therefore, Area of cube : ", area)
}

输出

Program to find the area of a cube

Dimension of the side : 5 
Therefore, Area of cube : 150

代码说明

  • var side float64 = 5, var area float64 = 0 − 在这一行中,我们声明了变量side和area。由于side和area将是float数据类型,所以我们使用了float64数据类型。

  • area = 6 * math.Pow(side, 2) − 我们使用公式area = 6 * (side) 2 来计算立方体的面积。math.Pow是一个数学函数,用于计算一个数的幂。

  • fmt.Println(“side的尺寸:”, side) − 打印立方体的边长。

  • fmt.Println(“因此,立方体的面积:”, area) − 打印计算得到的立方体的面积。

因此,面积 = 6 * (5) 2

面积 = 6 * 25

面积 = 150

使用不同函数计算立方体的面积

步骤

步骤1 − 声明一个变量来存储立方体的边长- ‘side’。

步骤2 − 声明一个变量来存储立方体的面积- ‘area’,并最初将其初始化为0。

步骤3 − 通过乘以边长的平方并将结果存储在函数calculateAreaOfCube()中的’area’变量中来计算面积。

步骤4 − 通过从main()函数中调用calculateAreaOfCube()函数来打印计算得到的面积,即存储在变量’area’中的值。

示例

package main
// fmt package allows us to print formatted strings
// math package allows us to perform various mathematical
// operations

import (
   "fmt"
   "math"
)
func calculateAreaOfCube(side float64) float64 {

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

   // calculating the area of the cube
   area = 6 * math.Pow(side, 2)
   return area
}
func main() {

   // declaring variable ‘side’ for storing length of cube
   var side float64 = 15
   var area float64
   fmt.Println("Program to find the area of a cube \n")

   // calling function calculateAreaOfCube() for calculating
   // the area of the cube
   area = calculateAreaOfCube(side)

   // printing the results
   fmt.Println("Dimension of the side : ", side)
   fmt.Println("Therefore, Area of cube : ", area)
}

输出

Program to find the area of a cube 

Dimension of the side :  15
Therefore, Area of cube :  1350

代码描述

  • var side float64 = 5, var area float64 = 0 - 在这行中,我们声明了side和area变量。由于side和area的数据类型为float,我们使用float64数据类型。

  • calculateAreaOfCube(side float64) float64 - 这是一个函数,我们计算立方体的面积。该函数有一个名为’side’的float64类型的参数,同时还有一个float64类型的返回值。

  • area = 6 * math.Pow(side, 2) - 我们使用这个公式来计算立方体的面积:area = 6 * (side)2。math.pow是一个数学函数,用于计算一个数的幂。

  • return area - 返回立方体的面积

  • area = calculateAreaOfCube(side) - 我们调用calculateAreaOfCube()函数,并将计算得到的值存储在’area’变量中。

  • fmt.Println(“Dimension of the side : :”, side) - 打印立方体的边长

  • fmt.Println(“Therefore, Area of cube : “, area) - 打印计算得到的立方体的面积

结论

这就是使用两种方法计算立方体的面积的全部内容。第二种方法在模块化和代码可重用性方面更好。您可以通过这些教程来更多地了解Golang编程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程