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("你出生在火星")
    	}
    }
    

      

  • 相关阅读:
    大话设计模式之代理模式
    大话设计模式之装饰者模式
    策略模式与简单工厂模式
    一个简单的使用Quartz和Oozie调度作业给大数据计算平台执行
    oozie JAVA Client 编程提交作业
    HashMap分析及散列的冲突处理
    cmp排序hdoj 1106排序
    定义member【C++】cstddef中4个定义
    目录启动CXF启动报告LinkageError异常以及Java的endorsed机制
    算法代码[置顶] 机器学习实战之KNN算法详解
  • 原文地址:https://www.cnblogs.com/yunweiqiang/p/11774707.html
Copyright © 2011-2022 走看看