Go语言流程控制02--选择结构之switch

package main

import "fmt"

/*
@星座诊所2(switch)
根据用户输入的出生月份猜测其星座:
·白羊(4) 金牛(5) 双子(6) 巨蟹(7) 狮子(8) 处女(9) 天平(10) 天蝎(11) 射手(12) 摩羯(1) 水瓶(2) 双鱼(3)
·使用单点case判断其星座;
·使用单点case集合判断其季节(春夏秋冬)
·使用自由条件case判断用户具体星座
*/
func main041() {
	fmt.Println("请输入你的出生月份(1-12)")
	var month int
	fmt.Scan(&month)
	fmt.Printf("month=%d", month)
	/*
	对month的可能取值,做单点判断
	*/
	switch month {
	case 1:
		fmt.Printf("你大概率是%s座\n", "摩羯")
	case 2:
		fmt.Printf("你大概率是%s座\n", "水瓶")
	case 3:
		fmt.Printf("你大概率是%s座\n", "双鱼")
	case 4:
		fmt.Printf("你大概率是%s座\n", "白羊")
	case 5:
		fmt.Printf("你大概率是%s座\n", "金牛")
	case 6:
		fmt.Printf("你大概率是%s座\n", "双子")
	case 7:
		fmt.Printf("你大概率是%s座\n", "巨蟹")
	case 8:
		fmt.Printf("你大概率是%s座\n", "狮子")
	case 9:
		fmt.Printf("你大概率是%s座\n", "处女")
	case 10:
		fmt.Printf("你大概率是%s座\n", "天平")
	case 11:
		fmt.Printf("你大概率是%s座\n", "天蝎")
	case 12:
		fmt.Printf("你大概率是%s座\n", "射手")

		//month的值没有落在上述任何一种情形中
		//default是可选的
	default:
		fmt.Println("你就是传说中的蛇夫座")
	}
}

/*
·使用单点case集合判断其季节(春夏秋冬)
*/
func main042() {
	fmt.Println("请输入你的出生月份(1-12)")
	var month int
	fmt.Scan(&month)
	fmt.Printf("month=%d", month)

	switch month {
	case 12, 1, 2:
		fmt.Println("你出生在冬天")
	case 3, 4, 5:
		fmt.Println("你出生在春天")
	case 6, 7, 8:
		fmt.Println("你出生在夏天")
	case 9, 10, 11:
		fmt.Println("你出生在秋天")
	default:
		fmt.Println("你出生在火星")
	}
}

/*
·使用自由条件case判断其季节(春夏秋冬)
*/
func main043() {
	fmt.Println("请输入你的出生月份(1-12)")
	var month int
	fmt.Scan(&month)

	// 没有明确指定switch的对象,case可以跟任意判断条件
	switch {
	case month >= 3 && month <= 5:
		fmt.Println("你出生在春天")
	case month >= 6 && month <= 8:
		fmt.Println("你出生在夏天")
	case month >= 9 && month <= 11:
		fmt.Println("你出生在秋天")
	case month == 12 || month == 1 || month == 2:
		fmt.Println("你出生在冬天")
	default:
		fmt.Println("你出生在火星")
	}
}

/*使用fallthrough强制滚动到下一个分支并执行*/
func main044() {
	fmt.Println("请输入你的出生月份(1-12)")
	var month int
	fmt.Scan(&month)

	// 没有明确指定switch的对象,case可以跟任意判断条件
	switch {
	case month >= 3 && month <= 5:
		fmt.Println("你出生在春天")
		//强制执行下一个分支条件
		fallthrough
	case month >= 6 && month <= 8:
		fmt.Println("你出生在夏天")
		fmt.Println("我们出生在上半年")
		fallthrough
	case month >= 9 && month <= 11:
		fmt.Println("你出生在秋天")
		//强制执行下一个分支条件
		fallthrough
	case month == 12 || month == 1 || month == 2:
		fmt.Println("你出生在冬天")
		fmt.Println("我们出生在下半年")

	default:
		fmt.Println("你出生在火星")
	}
}

  

猜你喜欢

转载自www.cnblogs.com/yunweiqiang/p/11774707.html