变量 s:="123"
要得到s中字符串 1+2+3 的值
package main
import (
"fmt"
"strconv"
)
func main() {
s:="123"
sum := 0
for k,_:=range s {
n ,_:= strconv.Atoi(string(s[k]))
sum+=n
}
fmt.Println(sum)
}
#6
更多评论
```
package main
import (
"strconv"
"fmt"
)
func main() {
var s = "987654321"
var result = 0
re, _ := strconv.Atoi(s)
for i := re; i > 0; i /= 10 {
result += i % 10
}
fmt.Println(result)
}
```
#1