使用Switch Case語句編寫Go語言簡單計算器程式


本教程將介紹如何在Go程式語言中使用switch case語句編寫一個簡單的計算器。

switch語句將評估一個表示式,將表示式的值與一系列給定的case條件進行比較,並執行第一個匹配值條件後的語句,直到遇到break語句。

Go語言基礎switch case語句及預設情況

  • switch語句執行第一個與輸入選擇相等的case。

  • case按照順序進行評估,一旦某個case成功匹配,則停止。

  • 如果沒有case(輸入的選擇)匹配,則執行default case中的語句。

如何在Go語言中使用switch case編寫簡單的計算器

語法

switch expression {
   case 1:
   // code block 1
	
   case 2:
   // code block 2
	
   ...
   ...
   default:
   // default code block
}

演算法

  • 步驟1 - 匯入fmt包

  • 步驟2 - 開始main()函式

  • 步驟3 - 宣告並初始化變數

  • 步驟4 - 建立switch case語句

  • 步驟5 - 使用內建函式fmt.Println()列印結果

示例

演示如何在Go語言程式中使用switch case編寫簡單的計算器

// Golang program to make a Simple // Calculator using Switch Case package main // fmt package provides the function to print anything import "fmt" // start the main() function func main() { // Declare amd initialize the variables var number1 int=20 var number2 int=10 var choice int = 0 // choice of the input calculation var x int // the result variable fmt.Println("number 1 = ",number1,"\nnumber 2 =",number2) fmt.Println(" choice 1: Addition of the two numbers") fmt.Println(" choice 2: Subtraction of the two numbers") fmt.Println(" choice 3: Multiplication of the two numbers") fmt.Println(" choice 4: Division of the two numbers") fmt.Scanln(&choice) // print the choice of calculation using switch case switch choice{ case 1: x=number1+number2 fmt.Printf("Addition of the two numbers is: %d",x) case 2: x=number1-number2 fmt.Printf("Subtraction of the two numbers is: %d",x) case 3: x=number1*number2 fmt.Printf("Multiplication of the two numbers is: %d",x) case 4: x=number1/number2 fmt.Printf("Division of the two numbers is: %d",x) default: fmt.Println("Invalid number") } // Print the result using built-in function fmt.Println() }

輸入

number 1 = 20
number 2 = 10
choice 1: Addition of the two numbers
choice 2: Subtraction of the two numbers
choice 3: Multiplication of the two numbers
choice 4: Division of the two numbers
2

輸出

Subtraction of the two numbers is: 10

程式碼描述

  • 在上面的程式中,我們首先宣告main包。

  • 我們匯入了fmt包,其中包含fmt包的檔案。

  • 現在開始main()函式。Go程式的執行從main()函式開始。

  • 宣告並初始化變數number1和number2,變數choice對應計算的選擇。變數x是結果整型變數。

  • 建立switch case語句來執行程式碼

  • 最後,我們使用內建函式fmt.Println()在螢幕上列印結果。此函式在fmt包中定義,用於寫入標準輸出。

如何在兩個單獨的函式中使用Switch Case編寫簡單的計算器

語法

func functionname(list_of_parameters)(return_type) {
   //...
   //function_body
}

演算法

  • 步驟1 - 匯入fmt包

  • 步驟2 - 建立calculator()函式

  • 步驟3 - 宣告並初始化變數

  • 步驟4 - 建立switch case語句

  • 步驟5 - 開始main()函式

  • 步驟6 - 呼叫calculator()函式

  • 步驟7 - 使用內建函式fmt.Println()列印結果。

示例

演示如何在兩個單獨的函式中使用Go語言程式中的switch case編寫簡單的計算器

// Golang program to make a Simple // Calculator using Switch Case package main // fmt package provides the function to print anything import "fmt" // Creating a function Calculator() func calculator(choice int) int { // declare and initialize the variables var result int var num1 int = 30 var num2 int = 15 // print the choice of calculation using switch case switch choice { case 1: result = num1 + num2 fmt.Printf("Addition is: %d \n", result) case 2: result = num1 - num2 fmt.Printf("Subtraction is: %d \n", result) case 3: result = num1 * num2 fmt.Printf("Multiplication is: %d \n", result) case 4: result = num1 / num2 fmt.Printf("Division is: %d \n", result) default: fmt.Println("Invalid value") } return 0 } // start the main() function func main() { fmt.Println("Number 1 = 30 \nNumber 2= 15") fmt.Println("Enter the following operation you want to perform") fmt.Println("1 for addition \n2 for Subtration \n3 for Multiplication \n4 for Division") var option int = 0 // calling the calculator() function fmt.Scanln(&option) calculator(option) // Print the result using built-in function fmt.Println() }

輸入

Number 1 = 30
Number 2= 15
Enter the following operation you want to perform
1 for addition
2 for Subtration
3 for Multiplication
4 for Division
2

輸出

Subtraction is: 15

程式碼描述

  • 在上面的程式中,我們首先宣告main包。

  • 我們匯入了fmt包,其中包含fmt包的檔案。

  • 建立calculator()函式來計算選擇

  • 宣告並初始化變數num1和num2。變數result是最終結果整型變數。

  • 建立switch case語句來根據輸入選擇執行程式碼

  • 接下來,我們開始main()函式。Go程式的執行從main()函式開始。

  • 這裡我們將使用使用者輸入函式 – fmt.Scanln(),然後我們呼叫calculator()函式來計算結果

  • 最終結果使用內建函式fmt.Println()列印到控制檯螢幕上。此函式在fmt包中定義,用於寫入標準輸出。

結論

在以上兩個示例中,我們已成功編譯並執行了Go語言程式碼,使用switch case編寫了一個簡單的計算器。

儘管我們可以使用if...else語句代替switch case語句,但使用switch case編寫的程式碼更加簡潔易寫。

更新於:2022年10月28日

4K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告