Go基础_1_11 switch
Tutorial: Go初级
Category: Go
Published: 2026-04-07 13:58:26
Views: 21
Likes: 0
Comments: 0
package main
import "fmt"
func main() {
switch {
case true:
fmt.Println("true..")
case false:
fmt.Println("false...")
}
score := 88
switch {
case score >= 0 && score < 60:
fmt.Println(score, "不及格")
case score >= 60 && score < 70:
fmt.Println(score, "及格")
case score >= 70 && score < 80:
fmt.Println(score, "中等")
case score >= 80 && score < 90:
fmt.Println(score, "良好")
case score >= 90 && score <= 100:
fmt.Println(score, "优秀")
default:
fmt.Println("成绩有误。。。")
}
fmt.Println("---------------------")
letter := ""
switch letter {
case "A", "E", "I", "O", "U":
fmt.Println(letter, "是元音。。")
case "M", "N":
fmt.Println("M或N。。")
default:
fmt.Println("其他。。")
}
month := 9
day := 0
year := 2019
switch month {
case 1, 3, 5, 7, 8, 10, 12:
day = 31
case 4, 6, 9, 11:
day = 30
case 2:
if year%400 == 0 || year%4 == 0 && year%100 != 0 {
day = 29
} else {
day = 28
}
default:
fmt.Println("月份有误。。")
}
fmt.Printf("%d 年 %d 月 的天数是:%d\n", year, month, day)
fmt.Println("--------------------------")
switch language := "golang"; language {
case "golang":
fmt.Println("Go语言。。")
case "java":
fmt.Println("Java语言。。")
case "python":
fmt.Println("Python语言。。")
}
}