Compare commits
2
Commits
50306fbe17
...
787dd02bb3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
787dd02bb3 | ||
|
|
e6db364f3e |
@@ -2,7 +2,7 @@ package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func exercise1() int {
|
||||
func exercise1() {
|
||||
fmt.Println("=== 练习1:基本for循环 ===")
|
||||
|
||||
sum := 0
|
||||
@@ -11,15 +11,14 @@ func exercise1() int {
|
||||
// 初始化: i := 1
|
||||
// 条件: i <= 10
|
||||
// 后置: i++
|
||||
for ____; ____; ____ {
|
||||
for i := 1; i <= 10; i++ {
|
||||
sum += i
|
||||
}
|
||||
|
||||
fmt.Printf("1到10的和: %d\n", sum)
|
||||
return sum
|
||||
}
|
||||
|
||||
func exercise2() int {
|
||||
func exercise2() {
|
||||
fmt.Println("\n=== 练习2:while形式的for循环 ===")
|
||||
|
||||
sum := 0
|
||||
@@ -27,13 +26,12 @@ func exercise2() int {
|
||||
|
||||
// 使用while形式的for循环,计算1到5的乘积
|
||||
// 提示:省略初始化和后置语句
|
||||
for ____ {
|
||||
for i <= 5 {
|
||||
sum *= i
|
||||
i++
|
||||
}
|
||||
|
||||
fmt.Printf("1到5的乘积: %d\n", sum)
|
||||
return sum
|
||||
}
|
||||
|
||||
func exercise3() []int {
|
||||
@@ -46,12 +44,12 @@ func exercise3() []int {
|
||||
// 如果数字大于8,停止循环(使用break)
|
||||
i := 1
|
||||
for i <= 10 {
|
||||
if ____ {
|
||||
if i%3 == 0 {
|
||||
i++
|
||||
____
|
||||
continue
|
||||
}
|
||||
if ____ {
|
||||
____
|
||||
if i > 8 {
|
||||
break
|
||||
}
|
||||
nums = append(nums, i)
|
||||
i++
|
||||
@@ -69,7 +67,7 @@ func exercise4() string {
|
||||
|
||||
// 使用for-range遍历切片,格式化输出
|
||||
// 提示:range会返回索引和值,示例: "0: 苹果"
|
||||
for ____, ____ := range ____ {
|
||||
for index, value := range fruits {
|
||||
result += fmt.Sprintf("%d: %s ", index, value)
|
||||
}
|
||||
|
||||
@@ -85,7 +83,7 @@ func exercise5() string {
|
||||
|
||||
// 使用for-range遍历map,格式化输出
|
||||
// 提示:range遍历map时,返回的是键和值
|
||||
for ____, ____ := range ____ {
|
||||
for name, score := range scores {
|
||||
result += fmt.Sprintf("%s: %d ", name, score)
|
||||
}
|
||||
|
||||
@@ -94,7 +92,7 @@ func exercise5() string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("=== for循环 练习 ===\n")
|
||||
fmt.Println("=== for循环 练习 ===")
|
||||
|
||||
exercise1()
|
||||
exercise2()
|
||||
|
||||
@@ -6,7 +6,7 @@ func exercise1() {
|
||||
fmt.Println("=== 练习1:defer基本用法 ===")
|
||||
|
||||
// 使用defer,在函数结束时打印"函数结束"
|
||||
____
|
||||
defer fmt.Println("函数结束")
|
||||
fmt.Println("函数执行中")
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ func exercise2() {
|
||||
|
||||
// 使用多个defer语句,按照1-2-3的顺序声明
|
||||
// 观察输出的顺序(应该是3-2-1)
|
||||
____
|
||||
____
|
||||
____
|
||||
defer fmt.Print(1)
|
||||
defer fmt.Print(2)
|
||||
defer fmt.Print(3)
|
||||
fmt.Println("正常执行")
|
||||
}
|
||||
|
||||
@@ -27,13 +27,13 @@ func exercise3() int {
|
||||
// 使用具名返回值result
|
||||
// defer函数中将result修改为100
|
||||
// return返回50
|
||||
var ____ int = ____
|
||||
var result int = 100
|
||||
|
||||
defer func() {
|
||||
____ = ____
|
||||
result = 50
|
||||
}()
|
||||
|
||||
return ____
|
||||
return result
|
||||
}
|
||||
|
||||
func exercise4() {
|
||||
@@ -43,7 +43,7 @@ func exercise4() {
|
||||
|
||||
// 使用defer打印i的值
|
||||
// 注意:defer声明时就对参数求值了
|
||||
____
|
||||
defer fmt.Println(i)
|
||||
|
||||
i = 10
|
||||
fmt.Println("修改i为:", i)
|
||||
@@ -58,7 +58,7 @@ func exercise5() {
|
||||
|
||||
// 使用defer在函数结束时关闭资源
|
||||
defer func() {
|
||||
fmt.Println("关闭:", ____)
|
||||
fmt.Println("关闭:", resource)
|
||||
}()
|
||||
|
||||
fmt.Println("使用:", resource)
|
||||
@@ -71,15 +71,15 @@ func exercise6() {
|
||||
// 每个defer捕获当前i的值并打印
|
||||
// 提示:使用闭包或立即求值
|
||||
for i := 1; i <= 3; i++ {
|
||||
____ func(n int) {
|
||||
defer func(n int) {
|
||||
fmt.Printf("defer输出: %d\n", n)
|
||||
}(____)
|
||||
}(i)
|
||||
fmt.Println("循环输出:", i)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("=== defer 练习 ===\n")
|
||||
fmt.Println("=== defer 练习 ===")
|
||||
|
||||
exercise1()
|
||||
exercise2()
|
||||
|
||||
Reference in New Issue
Block a user