Golang 流程控制(if、switch)
if判断
if
示例:
package main
import "fmt"
func main() {
var yes string
fmt.Print("有没有卖西瓜的:")
fmt.Scan(&yes)
fmt.Print("老婆的想法:买十个包子")
if yes == "y" || yes == "yes" || yes == "Y" || yes == "YES" {
fmt.Println("和一个西瓜")
}
fmt.Println("老公的想法:")
if yes == "y" || yes == "yes" || yes == "Y" || yes == "YES" {
fmt.Println("买一个包子")
} else {
fmt.Println("买十个包子")
}
}
标 准 输 出 : \color{#FF0000}{标准输出:} 标准输出:
F:\GO>go run f:\go\Process.go
有没有卖西瓜的:y
老婆的想法:买十个包子和一个西瓜
老公的想法:
买一个包子
F:\GO>go run f:\go\Process.go
有没有卖西瓜的:n
老婆的想法:买十个包子老公的想法:
买十个包子
elas if
示例:
package main
import "fmt"
func main() {
fmt.Print("你的成绩是:")
var num int
fmt.Scan(&num)
if num > 90 {
fmt.Println("A")
} else if num >= 80 {
fmt.Println("B")
} else if num >= 70 {
fmt.Println("C")
} else if num >= 60 {
fmt.Println("D")
} else {
fmt.Println("不及格")
}
}
标 准 输 出 : \color{#FF0000}{标准输出:} 标准输出: