switch

/*
	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语言")
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值