输入来自console124.0scanner := bufio.NewScanner(os.Stdin) // Declare second integer, double, and String variables. var in uint64 var fl float64 var st string // Read and save an integer, double, and String to your variables. var str string for scanner.Scan() { fmt.Println(scanner.Text()) }我如何使用第一个值并添加它来打印该结果?124
1 回答
梵蒂冈之花
TA贡献1900条经验 获得超5个赞
您还必须使用strconv来转换数字
scn := bufio.NewScanner(os.Stdin)
scn.Scan()
n1, err := strconv.Atoi(scn.Text())
if err != nil {
fmt.Printf("operation failed: %v\n", err)
return
}
scn.Scan()
n2, err := strconv.Atoi(scn.Text())
if err != nil {
fmt.Printf("operation failed: %v\n", err)
return
}
fmt.Printf("%d + %d = %d\n", n1, n2, n1+n2)
添加回答
举报
0/150
提交
取消