ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

44_Go基础_1_11 switch

2021-11-29 10:04:19  阅读:127  来源: 互联网

标签:11 case 44 数值 Println switch score Go fmt


  1 package main
  2 
  3 import "fmt"
  4 
  5 func main() {
  6     /*
  7         1.switch的标注写法:
  8         switch 变量{
  9         case 数值1:分支1
 10         case 数值2:分支2
 11         。。。
 12         default:
 13             最后一个分支
 14         }
 15         2.省略switch后的变量,相当于直接作用在true上
 16         switch{//true
 17         case true:
 18         case false:
 19         }
 20 
 21         3.case后可以同时跟随多个数值
 22         switch 变量{
 23         case 数值1,数值2,数值3:
 24 
 25         case 数值4,数值5:
 26 
 27         }
 28 
 29         4.switch后可以多一条初始化语句
 30         switch 初始化语句;变量{
 31         }
 32     */
 33     switch {
 34     case true:
 35         fmt.Println("true..")
 36     case false:
 37         fmt.Println("false...")
 38     }
 39     /*
 40         成绩:
 41         [0-59],不及格
 42         [60,69],及格
 43         [70,79],中
 44         [80,89],良好
 45         [90,100],优秀
 46     */
 47     score := 88
 48     switch {
 49     case score >= 0 && score < 60:
 50         fmt.Println(score, "不及格")
 51     case score >= 60 && score < 70:
 52         fmt.Println(score, "及格")
 53     case score >= 70 && score < 80:
 54         fmt.Println(score, "中等")
 55     case score >= 80 && score < 90:
 56         fmt.Println(score, "良好")
 57     case score >= 90 && score <= 100:
 58         fmt.Println(score, "优秀")
 59     default:
 60         fmt.Println("成绩有误。。。")
 61 
 62     }
 63 
 64     fmt.Println("---------------------")
 65     letter := ""
 66     switch letter {
 67     case "A", "E", "I", "O", "U":
 68         fmt.Println(letter, "是元音。。")
 69     case "M", "N":
 70         fmt.Println("M或N。。")
 71     default:
 72         fmt.Println("其他。。")
 73     }
 74     /*
 75         一个月的天数
 76         1,3,5,7,8,10,12
 77             31
 78         4,6,9,11
 79             30
 80         2:29/28
 81     */
 82     month := 9
 83     day := 0
 84     year := 2019
 85     switch month {
 86     case 1, 3, 5, 7, 8, 10, 12:
 87         day = 31
 88 
 89     case 4, 6, 9, 11:
 90         day = 30
 91     case 2:
 92         if year%400 == 0 || year%4 == 0 && year%100 != 0 {
 93             day = 29
 94         } else {
 95             day = 28
 96         }
 97     default:
 98         fmt.Println("月份有误。。")
 99     }
100     fmt.Printf("%d 年 %d 月 的天数是:%d\n", year, month, day)
101     fmt.Println("--------------------------")
102 
103     switch language := "golang"; language {
104     case "golang":
105         fmt.Println("Go语言。。")
106     case "java":
107         fmt.Println("Java语言。。")
108     case "python":
109         fmt.Println("Python语言。。")
110     }
111     //fmt.Println(language) //undefined: language
112 }

 

标签:11,case,44,数值,Println,switch,score,Go,fmt
来源: https://www.cnblogs.com/luwei0915/p/15618037.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有