zoukankan      html  css  js  c++  java
  • 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座
    ", "摩羯")
    	case 2:
    		fmt.Printf("你大概率是%s座
    ", "水瓶")
    	case 3:
    		fmt.Printf("你大概率是%s座
    ", "双鱼")
    	case 4:
    		fmt.Printf("你大概率是%s座
    ", "白羊")
    	case 5:
    		fmt.Printf("你大概率是%s座
    ", "金牛")
    	case 6:
    		fmt.Printf("你大概率是%s座
    ", "双子")
    	case 7:
    		fmt.Printf("你大概率是%s座
    ", "巨蟹")
    	case 8:
    		fmt.Printf("你大概率是%s座
    ", "狮子")
    	case 9:
    		fmt.Printf("你大概率是%s座
    ", "处女")
    	case 10:
    		fmt.Printf("你大概率是%s座
    ", "天平")
    	case 11:
    		fmt.Printf("你大概率是%s座
    ", "天蝎")
    	case 12:
    		fmt.Printf("你大概率是%s座
    ", "射手")
    
    		//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("你出生在火星")
    	}
    }
    

      

  • 相关阅读:
    多线程008如何预防死锁
    多线程003volatile的可见性和禁止指令重排序怎么实现的
    多线程011线程池运行原理及复用原理
    多线程010为什么要使用线程池
    多线程009什么是守护线程
    多线程005创建线程有哪些方式
    多线程007描述一下线程安全活跃态问题,以及竞态条件
    多线程002ThreadLocal有哪些内存泄露问题,如何避免
    关于Tomcat的启动时机(精通Eclipse Web开发P40)
    乱侃~~~
  • 原文地址:https://www.cnblogs.com/yunweiqiang/p/11774707.html
Copyright © 2011-2022 走看看