为什么我的循环执行命令的顺序与描述的顺序不一致?

huangapple go评论65阅读模式
英文:

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 (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;os&quot;
)

func main() {
	var wallCount int
	var width, height, area float64
	var r = bufio.NewReader(os.Stdin)
	fmt.Print(&quot;WallCount:&quot;)
	fmt.Scanf(&quot;%d&quot;, &amp;wallCount)
	fmt.Printf(&quot;wallcount =  %v \n&quot;, wallCount)
	for x := 1; x &lt;= wallCount; x++ {
		fmt.Printf(&quot;wight, height at %v times\n&quot;, x)
		fmt.Fscanf(r, &quot;%d %d&quot;, &amp;width, &amp;height)
		area += width * height
	}

	fmt.Printf(&quot;area =  %v\n&quot;, 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

  1. why my for loops running first cmd twice then running second cmd then again first cmd twice and finally runs the last cmd?
  2. 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, &quot;%d %d&quot;, &amp;width, &amp;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(&quot;WallCount:&quot;)
	fmt.Scanf(&quot;%d&quot;, &amp;wallCount)
	fmt.Printf(&quot;wallcount =  %v \n&quot;, wallCount)
	for x := 1; x &lt;= wallCount; x++ {
		fmt.Printf(&quot;wight, height at %v times\n&quot;, x)
		_, err := fmt.Fscanf(r, &quot;%f %f\n&quot;, &amp;width, &amp;height)
		if err != nil {
			log.Println(err)
            return
		}
		area += width * height
	}

	fmt.Printf(&quot;area =  %v\n&quot;, area)
}

In this case, the error very clearly described what was wrong, namely: bad verb &#39;%d&#39; for float64. In go this form of checking if an error is nil is very common, and you should always check your errors.

huangapple
  • 本文由 发表于 2022年5月23日 19:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/72347497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定