Golang程序 使用Switch Case制作一个简单的计算器
在本教程中,我们将看到如何在Go编程语言中使用switch case语句制作一个简单的计算器。
switch语句将评估一个表达式,将表达式的值与给定的一系列case条件进行比较,并在第一个有匹配值的条件后执行语句,直到遇到中断。
Golang的基本switch case,带有默认值
- switch语句运行与输入的选择相等的第一个案例。
-
案例按顺序被评估,当一个案例成功时,它就停止。
-
如果没有匹配的案例(输入的选择),它就是一个默认案例,其语句被执行。
如何使用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()打印结果。
例子
展示了如何在golang程序中使用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,变量的选择与计算的选择相对应。变量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()打印结果。
例子
展示了如何在golang程序中使用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包下,它有助于编写标准输出。
总结
在上面的两个例子中,我们已经成功地编译并执行了Golang代码,使用switch case制作一个简单的计算器。
尽管我们可以用if…else语句来代替switch case语句,但用switch case编写的代码更简洁,更容易编写。
极客教程