求变量 s:="123" 中123相加求和的方法

china_wenya · · 1477 次点击
另一种思路 ``` package main import ( "fmt" "strconv" ) func main() { s := "123" var total int for i :=0; i< len(s) ; i++ { b, _ := strconv.Atoi(string(s[i])) //log.Println(b) total += b } fmt.Printf("%T %v \n", total, total) } ```
#3
更多评论
``` 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