golang-学习记录-switch几种语法

package main

import (
"fmt"
)
func main() {
sin:=3
grade:="B"
marks:=81
var x interface{}
//使用 fallthrough 会强制执行后面的 case 语句,fallthrough 不会判断下一条 case 的表达式结果是否为 true
switch sin {
case 1:
fmt.Printf("sin : %d\n",sin)
fallthrough
case 2:
fmt.Printf("sin : %d\n",sin)
fallthrough
case 3:
fmt.Printf("sin : %d\n",sin)
fallthrough
default:
fmt.Printf("sin : %d\n",sin)
}
//switch 语句还可以被用于 type-switch 来判断某个 interface 变量中实际存储的变量类型
switch i := x.(type) {
case nil:fmt.Printf(" x 的类型 :%T\n",i)
case int:fmt.Printf("x 是 int 型")
case float64:fmt.Printf("x 是 float64 型")
case func(int):fmt.Printf("x 是 func(int) 型")
case bool,string:fmt.Printf("x 是 bool 或 string 型")
default :fmt.Printf("x 是 bool 或 string 型")
}

switch {
case
marks > 90:grade = "A"
case
marks > 80 && marks < 90:grade = "B"
case
marks > 50 && marks < 70:grade = "C"
default:
grade="D"
}

switch grade{
case "A":
fmt.Printf("优秀!\n")
case "B":
fmt.Printf("良好!\n")
case "C":
fmt.Printf("及格!\n")
case "D":
fmt.Printf("不及格!\n")
default:
fmt.Printf("差\n")

}
fmt.Printf("你的等级是 %s",grade)

}

 

posted @ 2021-08-05 16:32  sin涛涛  阅读(79)  评论(0)    收藏  举报