Go語言程式計算立方體的面積


在本教程中,我們將討論在 Go 語言程式設計中使用立方體的邊長來查詢立方體表面積的方法。

但在編寫程式碼之前,讓我們簡要討論一下立方體及其面積。

立方體

立方體是一個三維圖形,具有六個正方形面。立方體的六個面都是正方形形狀。它的長、寬和高都相等。骰子是立方體的一個常見例子。

立方體的面積

立方體所有六個面的總面積稱為立方體的表面積。在我們需要為立方體的表面塗漆或在其周圍包裹一層薄膜的情況下,計算立方體的面積會很有用。


公式

立方體所有面的面積之和定義為立方體的表面積。因此,公式可以寫成邊長的平方乘以6。

$$\mathrm{面積\, =\, 6\ast \left ( 邊長 \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 − 宣告一個變數用於儲存立方體的邊長 - ‘邊長’。

步驟 2 − 宣告一個變數用於儲存立方體的面積 - ‘面積’,並將其初始化為 0。

步驟 3 − 透過將邊長的平方乘以 6 來計算面積,並將結果儲存在函式內的 ‘面積’ 變數中。

步驟 4 − 列印計算出的面積,即儲存在變數 ‘面積’ 中的值。

示例

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 的資料型別為浮點數,因此我們使用 float64 資料型別。

  • area = 6 * math.Pow(side, 2) − 我們使用公式 area = 6 * (side)2 來查詢立方體的面積。math.Pow 是一個用於計算數字冪的數學函式。

  • fmt.Println("邊長的尺寸:", side) − 列印立方體的邊長。

  • fmt.Println("因此,立方體的面積:", area) − 列印計算出的立方體的面積。

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

面積 = 6 * 25

面積 = 150

使用不同的函式查詢立方體的面積

演算法

步驟 1 − 宣告一個變數用於儲存立方體的邊長 - ‘邊長’。

步驟 2 − 宣告一個變數用於儲存立方體的面積 - ‘面積’,並將其初始化為 0。

步驟 3 − 透過將邊長的平方乘以 6 來計算面積,並將結果儲存在函式 calculateAreaOfCube() 內的 ‘面積’ 變數中。

步驟 4 − 透過從 main() 函式內部呼叫 calculateAreaOfCube() 函式來列印計算出的面積,即儲存在變數 ‘面積’ 中的值。

示例

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 的資料型別為浮點數,因此我們使用 float64 資料型別。

  • calculateAreaOfCube(side float64) float64 − 這是我們計算立方體面積的函式。該函式將資料型別為 float64 的變數 ‘邊長’ 作為引數,並且還具有 float64 的返回型別。

  • area = 6 * math.Pow(side, 2) − 我們使用此公式 area = 6 * (side)2 來查詢立方體的面積。math.pow 是一個用於計算數字冪的數學函式。

  • return area − 用於返回立方體的面積

  • area = calculateAreaOfCube(side) − 我們正在呼叫函式 calculateAreaOfCube() 並將計算出的值儲存在 ‘面積’ 變數中。

  • fmt.Println("邊長的尺寸:", side) − 列印立方體的邊長

  • fmt.Println("因此,立方體的面積:", area) − 列印計算出的立方體的面積

結論

這就是使用兩種方法計算立方體面積的全部內容。就模組化和程式碼可重用性而言,第二種方法要好得多。您可以使用這些教程進一步探索 Go 語言程式設計。

更新於: 2022-11-21

245 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告