计分板上的分数是大家手打的字。写 TotalScores(texts):空字符串 "" 是没来的人,跳过;有一个不是数,就马上返回 0 和 strconv.Atoi 给的那个错误;都没问题就返回总分和 nil。
TotalScores([]string{"8", "", "7"})→(15, None)TotalScores([]string{"9", "ten"})→(0, 'strconv.Atoi: parsing "ten": invalid syntax')func TotalScores(texts []string) (int, error)package main
import "strconv"
func TotalScores(texts []string) (int, error) {
total := 0
for _, t := range texts {
// 在这里写:t 是空字符串就跳过
n, err := strconv.Atoi(t)
if err != nil {
// 在这里写:返回 0 和 err
}
total += n
}
return total, nil
}
写好了先点「运行」试试示例,没问题再点「提交」。