Golang 程序读取数字 (n) 并计算 (n+nn+nnn)
让我们读一个数字,n=5
然后,nn=55然后nnn=555
res=5+55+555=>615
要读取数字(n)并计算(n+nn+nnn),我们可以采用以下方法
脚步
定义一个变量,n。
打印一条语句以获取数字n。
获取变量n的用户输入。
为(n+nn+nnn)做一个表达式。
将表达式转换为数字。
计算表达式的总和。
示例
package main
import (
"fmt"
"strconv"
)
func main(){
var n int
fmt.Print("输入n的值: ")
fmt.Scanf("%d", &n)
t1 := fmt.Sprintf("%d", n)
t2 := fmt.Sprintf("+%d%d", n, n)
t3 := fmt.Sprintf("+%d%d%d", n, n, n)
exp := t1 + t2 + t3
fmt.Println("表达式为: ", exp)
n1, _ := strconv.Atoi(t1)
n2, _ := strconv.Atoi(t2)
n3, _ := strconv.Atoi(t3)
fmt.Println("Computed 表达式为: ", n1+n2+n3)
}输出结果输入n的值: 5 表达式为: 5+55+555 Computed 表达式为: 615
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下