Finish: 04-operators - 运算符

This commit is contained in:
2026-03-14 18:49:38 +08:00
parent 86c554908e
commit 2805379586
+19 -19
View File
@@ -9,10 +9,10 @@ func exercise1() {
// 使用算术运算符计算以下值: // 使用算术运算符计算以下值:
sum := a + b // 加法 sum := a + b // 加法
diff := ____ // 减法 diff := a - b // 减法
product := __ // 乘法 product := a * b // 乘法
quotient := __ // 除法 quotient := a / b // 除法
remainder := __ // 取余 remainder := a % b // 取余
fmt.Printf("%d + %d = %d\n", a, b, sum) fmt.Printf("%d + %d = %d\n", a, b, sum)
fmt.Printf("%d - %d = %d\n", a, b, diff) fmt.Printf("%d - %d = %d\n", a, b, diff)
@@ -27,13 +27,13 @@ func exercise2() {
x, y := 5, 10 x, y := 5, 10
// 使用关系运算符,判断x是否小于y // 使用关系运算符,判断x是否小于y
isLess := ____ isLess := x < y
// 使用关系运算符,判断x是否等于y // 使用关系运算符,判断x是否等于y
isEqual := ____ isEqual := x == y
// 使用关系运算符,判断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, isLess)
fmt.Printf("%d == %d: %t\n", x, y, isEqual) fmt.Printf("%d == %d: %t\n", x, y, isEqual)
@@ -47,13 +47,13 @@ func exercise3() {
isAdmin := false isAdmin := false
// 使用逻辑运算符 &&,判断"有权限且是管理员"的条件 // 使用逻辑运算符 &&,判断"有权限且是管理员"的条件
canDelete := ____ && ____ canDelete := hasPermission && isAdmin
// 使用逻辑运算符 ||,判断"有权限或是管理员"的条件 // 使用逻辑运算符 ||,判断"有权限或是管理员"的条件
canView := ____ || ____ canView := hasPermission || isAdmin
// 使用逻辑运算符 !,对isAdmin取反 // 使用逻辑运算符 !,对isAdmin取反
isNotAdmin := ____ isNotAdmin := !isAdmin
fmt.Printf("有权限: %t, 是管理员: %t\n", hasPermission, isAdmin) fmt.Printf("有权限: %t, 是管理员: %t\n", hasPermission, isAdmin)
fmt.Printf("可以删除: %t, 可以查看: %t\n", canDelete, canView) fmt.Printf("可以删除: %t, 可以查看: %t\n", canDelete, canView)
@@ -66,11 +66,11 @@ func exercise4() {
count := 5 count := 5
// 使用自增运算符,count加1 // 使用自增运算符,count加1
____ count ++
fmt.Printf("自增后: %d\n", count) fmt.Printf("自增后: %d\n", count)
// 使用自减运算符,count减1 // 使用自减运算符,count减1
____ count --
fmt.Printf("自减后: %d\n", count) fmt.Printf("自减后: %d\n", count)
} }
@@ -80,13 +80,13 @@ func exercise5() {
num := 10 num := 10
// 使用 += 运算符,给num加5 // 使用 += 运算符,给num加5
num ____ num += 5
// 使用 *= 运算符,将num乘2 // 使用 *= 运算符,将num乘2
num ____ num *= 2
// 使用 /= 运算符,将num除4 // 使用 /= 运算符,将num除4
num ____ num /= 4
fmt.Printf("最终值: %d\n", num) fmt.Printf("最终值: %d\n", num)
} }
@@ -98,16 +98,16 @@ func exercise6() {
a, b := 5, 3 a, b := 5, 3
// 使用位运算符 & 进行按位与 // 使用位运算符 & 进行按位与
andResult := ____ andResult := a & b
// 使用位运算符 | 进行按位或 // 使用位运算符 | 进行按位或
orResult := ____ orResult := a | b
// 使用位运算符 ^ 进行按位异或 // 使用位运算符 ^ 进行按位异或
xorResult := ____ xorResult := a ^ b
// 使用位运算符 << 将a左移1位(相当于乘以2) // 使用位运算符 << 将a左移1位(相当于乘以2)
leftShift := ____ leftShift := a << 1
fmt.Printf("%d & %d = %d\n", a, b, andResult) fmt.Printf("%d & %d = %d\n", a, b, andResult)
fmt.Printf("%d | %d = %d\n", a, b, orResult) fmt.Printf("%d | %d = %d\n", a, b, orResult)