Go語言程式計算球體的體積和表面積
在本教程中,我們將討論使用 Go 語言程式設計根據球體半徑計算球體體積和表面積的方法。
但在編寫程式碼之前,讓我們簡要討論一下球體及其體積和表面積。
球體
球體是一個三維圖形,其所有點都與中心等距。它沒有邊也沒有面。球體的半徑用“r”表示。球是一個球體的很好的例子。

球體的體積
球體佔據的空間容量或數量稱為其體積。計算球體的體積在我們需要知道足球中可以填充多少空氣的情況下非常有用。
$$\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 − 這是我們計算球體體積的函式。該函式具有資料型別為 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 語言程式設計的資訊
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP