Finish: 03-maps - map使用
This commit is contained in:
@@ -7,13 +7,13 @@ func exercise1() map[string]int {
|
||||
|
||||
// 创建一个map,键为string类型,值为int类型
|
||||
// 添加"张三": 90, "李四": 85, "王五": 95
|
||||
scores := ____
|
||||
scores := make(map[string]int)
|
||||
|
||||
scores["张三"] = ____
|
||||
scores["李四"] = ____
|
||||
scores["王五"] = ____
|
||||
scores["张三"] = 90
|
||||
scores["李四"] = 85
|
||||
scores["王五"] = 95
|
||||
|
||||
return ____
|
||||
return scores
|
||||
}
|
||||
|
||||
func exercise2() string {
|
||||
@@ -25,7 +25,7 @@ func exercise2() string {
|
||||
"李四": 85,
|
||||
}
|
||||
|
||||
score, exists := ____
|
||||
score, exists := scores["李四"]
|
||||
|
||||
if exists {
|
||||
fmt.Printf("李四的分数是: %d\n", score)
|
||||
@@ -45,13 +45,13 @@ func exercise3() int {
|
||||
"赵六": 88,
|
||||
}
|
||||
|
||||
sum := ____
|
||||
sum := 0
|
||||
|
||||
for ____, score := range ____ {
|
||||
sum += ____
|
||||
for _, score := range scores {
|
||||
sum += score
|
||||
}
|
||||
|
||||
return ____
|
||||
return sum
|
||||
}
|
||||
|
||||
func exercise4() map[string]int {
|
||||
@@ -64,9 +64,9 @@ func exercise4() map[string]int {
|
||||
"王五": 95,
|
||||
}
|
||||
|
||||
____(scores, "李四")
|
||||
delete(scores, "李四")
|
||||
|
||||
return ____
|
||||
return scores
|
||||
}
|
||||
|
||||
func exercise5() []string {
|
||||
@@ -81,19 +81,19 @@ func exercise5() []string {
|
||||
"钱七": 92,
|
||||
}
|
||||
|
||||
var topStudents ____
|
||||
var topStudents []string
|
||||
|
||||
for name, score := range ____ {
|
||||
if score >= ____ {
|
||||
topStudents = append(____, ____)
|
||||
for name, score := range scores {
|
||||
if score >= 90 {
|
||||
topStudents = append(topStudents, name)
|
||||
}
|
||||
}
|
||||
|
||||
return ____
|
||||
return topStudents
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("=== map 练习 ===\n")
|
||||
fmt.Println("=== map 练习 ===")
|
||||
|
||||
result1 := exercise1()
|
||||
fmt.Printf("练习1结果: %v\n", result1)
|
||||
|
||||
Reference in New Issue
Block a user