From 971f2a7876bf2faa5c5e2ddd19a9f047cb2ceb42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=83=E5=96=83=E6=B1=A1?= Date: Thu, 6 Nov 2025 22:57:50 +0800 Subject: [PATCH] =?UTF-8?q?switch=E5=88=86=E6=94=AF1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 9ddd365..02c9d2b 100644 --- a/main.go +++ b/main.go @@ -3,19 +3,30 @@ package main import "fmt" func main() { - // 指定变量类型并且赋值 - var num1 int = 18 - fmt.Println(num1) + score := 87 + switch score / 10 { + case 10: + fmt.Println("You are 10%") + case 9: + fmt.Println("You are 9%") + case 8: + fmt.Println("You are 8%") + case 7: + fmt.Println("You are 7%") + case 6: + fmt.Println("You are 6%") + case 5: + fmt.Println("You are 5%") + case 4: + fmt.Println("You are 4%") + case 3: + fmt.Println("You are 3%") + case 2: + fmt.Println("You are 2%") + case 1: + fmt.Println("You are 1%") - // 指定变量类型,但不赋值,使用默认值 - var num2 int - fmt.Println(num2) - - // 自动类型推断 - var num3 = "tom" - fmt.Println(num3) - - // 省略var =写为:= - num4 := "男" - fmt.Println(num4) + default: + fmt.Println("You are 0%") + } }