From 2805379586af6f62b44ebdee89357299707b0b41 Mon Sep 17 00:00:00 2001 From: wonder Date: Sat, 14 Mar 2026 18:49:38 +0800 Subject: [PATCH] =?UTF-8?q?Finish:=2004-operators=20-=20=E8=BF=90=E7=AE=97?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stage01-basics/04-operators/main.go | 38 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/stage01-basics/04-operators/main.go b/stage01-basics/04-operators/main.go index 6601c45..a4d21e5 100644 --- a/stage01-basics/04-operators/main.go +++ b/stage01-basics/04-operators/main.go @@ -9,10 +9,10 @@ func exercise1() { // 使用算术运算符计算以下值: sum := a + b // 加法 - diff := ____ // 减法 - product := __ // 乘法 - quotient := __ // 除法 - remainder := __ // 取余 + diff := a - b // 减法 + product := a * b // 乘法 + quotient := a / b // 除法 + remainder := a % b // 取余 fmt.Printf("%d + %d = %d\n", a, b, sum) fmt.Printf("%d - %d = %d\n", a, b, diff) @@ -27,13 +27,13 @@ func exercise2() { x, y := 5, 10 // 使用关系运算符,判断x是否小于y - isLess := ____ + isLess := x < y // 使用关系运算符,判断x是否等于y - isEqual := ____ + isEqual := x == y // 使用关系运算符,判断x是否不等于y - isNotEqual := ____ + isNotEqual := x != y fmt.Printf("%d < %d: %t\n", x, y, isLess) fmt.Printf("%d == %d: %t\n", x, y, isEqual) @@ -47,13 +47,13 @@ func exercise3() { isAdmin := false // 使用逻辑运算符 &&,判断"有权限且是管理员"的条件 - canDelete := ____ && ____ + canDelete := hasPermission && isAdmin // 使用逻辑运算符 ||,判断"有权限或是管理员"的条件 - canView := ____ || ____ + canView := hasPermission || isAdmin // 使用逻辑运算符 !,对isAdmin取反 - isNotAdmin := ____ + isNotAdmin := !isAdmin fmt.Printf("有权限: %t, 是管理员: %t\n", hasPermission, isAdmin) fmt.Printf("可以删除: %t, 可以查看: %t\n", canDelete, canView) @@ -66,11 +66,11 @@ func exercise4() { count := 5 // 使用自增运算符,count加1 - ____ + count ++ fmt.Printf("自增后: %d\n", count) // 使用自减运算符,count减1 - ____ + count -- fmt.Printf("自减后: %d\n", count) } @@ -80,13 +80,13 @@ func exercise5() { num := 10 // 使用 += 运算符,给num加5 - num ____ + num += 5 // 使用 *= 运算符,将num乘2 - num ____ + num *= 2 // 使用 /= 运算符,将num除4 - num ____ + num /= 4 fmt.Printf("最终值: %d\n", num) } @@ -98,16 +98,16 @@ func exercise6() { a, b := 5, 3 // 使用位运算符 & 进行按位与 - andResult := ____ + andResult := a & b // 使用位运算符 | 进行按位或 - orResult := ____ + orResult := a | b // 使用位运算符 ^ 进行按位异或 - xorResult := ____ + xorResult := a ^ b // 使用位运算符 << 将a左移1位(相当于乘以2) - leftShift := ____ + leftShift := a << 1 fmt.Printf("%d & %d = %d\n", a, b, andResult) fmt.Printf("%d | %d = %d\n", a, b, orResult)