英文:
Why does my loop execute commands not in the order they are described
问题
现在我正在尝试学习Go语言。
我有以下代码:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
var wallCount int
var width, height, area float64
var r = bufio.NewReader(os.Stdin)
fmt.Print("WallCount:")
fmt.Scanf("%d", &wallCount)
fmt.Printf("wallcount = %v \n", wallCount)
for x := 1; x <= wallCount; x++ {
fmt.Printf("wight, height at %v times\n", x)
fmt.Fscanf(r, "%d %d", &width, &height)
area += width * height
}
fmt.Printf("area = %v\n", area)
}
当我编译代码时,在终端上会出现以下内容:
WallCount:
输入到终端4:
WallCount:4
---
wallcount = 4
wight, height at 1 times
wight, height at 2 times
输入到终端1,1:
WallCount:4
wallcount = 4
wight, height at 1 times
wight, height at 2 times
1,1
---
wight, height at 3 times
wight, height at 4 times
area = 0
请问:
1)为什么我的for循环
会先运行第一个命令两次,然后运行第二个命令,然后再次运行第一个命令两次,最后运行最后一个命令?
2)为什么area
的值为0?
英文:
Now i'm trying to learn go.
I'm have code:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
var wallCount int
var width, height, area float64
var r = bufio.NewReader(os.Stdin)
fmt.Print("WallCount:")
fmt.Scanf("%d", &wallCount)
fmt.Printf("wallcount = %v \n", wallCount)
for x := 1; x <= wallCount; x++ {
fmt.Printf("wight, height at %v times\n", x)
fmt.Fscanf(r, "%d %d", &width, &height)
area += width * height
}
fmt.Printf("area = %v\n", area)
}
When i'm compiling code
On a terminal:
WallCount:
passing to term 4
WallCount:4
---
wallcount = 4
wight, height at 1 times
wight, height at 2 times
passing to term 1,1
WallCount:4
wallcount = 4
wight, height at 1 times
wight, height at 2 times
1,1
---
wight, height at 3 times
wight, height at 4 times
area = 0
Can u please explain me
- why my
for loops
running first cmd twice then running second cmd then again first cmd twice and finally runs the last cmd? - why
area
contains 0 ?
答案1
得分: 2
这里有几个问题。首先,你使用了%d
,它表示整数值,而你正在使用浮点数值(应该使用%f
)。
这个函数:fmt.Fscanf(r, "%d %d", &width, &height)
返回两个值。第一个值是成功解析的项目数,第二个值是一个错误。你应该始终检查返回的错误是否不为nil
,这意味着发生了错误:
func main() {
var wallCount int
var width, height, area float64
var r = bufio.NewReader(os.Stdin)
fmt.Print("WallCount:")
fmt.Scanf("%d", &wallCount)
fmt.Printf("wallcount = %v \n", wallCount)
for x := 1; x <= wallCount; x++ {
fmt.Printf("wight, height at %v times\n", x)
_, err := fmt.Fscanf(r, "%f %f\n", &width, &height)
if err != nil {
log.Println(err)
return
}
area += width * height
}
fmt.Printf("area = %v\n", area)
}
在这种情况下,错误非常清楚地描述了问题,即:bad verb '%d' for float64
。在Go语言中,这种检查错误是否为nil的形式非常常见,你应该始终检查错误。
英文:
There are a few things going wrong here. First of all, you used %d
, which denotes an integer value, while you are using float values (which use %f
).
This function: fmt.Fscanf(r, "%d %d", &width, &height)
returns two values. The first value is the number of items it has successfully parsed, and the second item is an error. You should always check if the error returned is not nil
, this means that there was an error:
func main() {
var wallCount int
var width, height, area float64
var r = bufio.NewReader(os.Stdin)
fmt.Print("WallCount:")
fmt.Scanf("%d", &wallCount)
fmt.Printf("wallcount = %v \n", wallCount)
for x := 1; x <= wallCount; x++ {
fmt.Printf("wight, height at %v times\n", x)
_, err := fmt.Fscanf(r, "%f %f\n", &width, &height)
if err != nil {
log.Println(err)
return
}
area += width * height
}
fmt.Printf("area = %v\n", area)
}
In this case, the error very clearly described what was wrong, namely: bad verb '%d' for float64
. In go this form of checking if an error is nil is very common, and you should always check your errors.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论