/*
1.switch语句:
语法结构:
switch 变量名{
case 数值1:分支1
case 数值2:分支2
case 数值3:分支3
...
default:最后一个分支
}
注意:
1.switch可以作用在其他类型上,case后的数值类型必须和switch作用的变量类型一致
2.case是无无序的
3.case后面的数值是唯一的
4.default语句是可选的操作,相当于
2.省略switch后的变量,相当于直接作用在true上
3.case后可以跟随多个数值
switch 变量{
case 数值一,数值二,数值三:
case 数值四,数值五:
}
4.switch后可以多一条初始化语句
switch 初始化语句;变量{}
*/
switch {
case true:
fmt.Println("true")
case false:
fmt.Println("false")
}
score := 80
switch {
case score >= 0 && score < 60:
fmt.Println("不及格")
case score >= 60 && score < 70:
fmt.Println("及格")
case score >= 70 && score < 90:
fmt.Println("良")
case score >= 90:
fmt.Println("优秀")
}
year := 0
month := 0
fmt.Println("计算某月天数")
fmt.Println("请输入年:")
fmt.Scanln(&year)
fmt.Println("请输入月:")
fmt.Scanln(&month)
switch month {
case 1, 3, 5, 7, 8, 10, 12:
fmt.Println("%d年%d月有%d天", year, month, 31)
case 4, 6, 9, 11:
fmt.Printf("%d年%d月有%d天", year, month, 30)
case 2:
if year%400 == 0 && year%4 == 0 && year%100 != 0 {
fmt.Println("%d年%d月有%d天", year, month, 29)
} else {
fmt.Println("%d年%d月有%d天", year, month, 28)
}
}
fmt.Println("-----------------")
switch language := "golang"; language {
case "golang":
fmt.Println("go语言")
case "java":
fmt.Println("java语言")
case "python":
fmt.Println("python语言")
}
switch
最新推荐文章于 2024-02-08 17:51:47 发布