dongxieyi9115
2012-12-22 04:33 阅读 238
已采纳

如何在Go中使用fmt.Scanf

I seem to be having a queer problem while getting user input within a for loop in go. Here is my code

package main

import "fmt"

func main() {
    var num int
    for i := 0; i < 10; i++ {
        fmt.Printf("Debug: i : %d ", i)
        fmt.Scanf("%d", &num)
        fmt.Println(num)
    }
}

What happens when I run this code is this :

Debug: i : 0
Enter next number
1
1
Debug: i : 1
Enter next number
1
Debug: i : 2
Enter next number
2
2
Debug: i : 3
Enter next number
2
Debug: i : 4
Enter next number
3
3
Debug: i : 5
Enter next number
3
Debug: i : 6
Enter next number
4
4
Debug: i : 7
Enter next number
4
Debug: i : 8
Enter next number
5
5
Debug: i : 9
Enter next number
5

What I notice is that each iteration of the loop happens twice, Could this be because Go is using parallelism by default and causing both processors to run the code within a for loop?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dongzhang4301 dongzhang4301 2012-12-22 07:35

    What OS are you using? Windows?

    Try this:

    package main
    
    import "fmt"
    
    func main() {
        var num int
        for i := 0; i < 10; i++ {
            fmt.Printf("Debug: i : %d
    ", i)
            fmt.Println("Enter next number")
            n, err := fmt.Scanf("%d
    ", &num)
            if err != nil {
                fmt.Println(n, err)
            }
            fmt.Println(num)
        }
    }
    

    Output:

    Debug: i : 0
    Enter next number
    1
    1
    Debug: i : 1
    Enter next number
    2
    2
    Debug: i : 2
    Enter next number
    3
    3
    Debug: i : 3
    Enter next number
    4
    4
    Debug: i : 4
    Enter next number
    5
    5
    Debug: i : 5
    Enter next number
    6
    6
    Debug: i : 6
    Enter next number
    7
    7
    Debug: i : 7
    Enter next number
    8
    8
    Debug: i : 8
    Enter next number
    9
    9
    Debug: i : 9
    Enter next number
    10
    10
    
    点赞 评论 复制链接分享
  • doubishi8303 doubishi8303 2012-12-23 02:56

    The above answer is a good suggestion. the code

        if err != nil {
            fmt.Println(n, err)
        }
    

    will output the reason of this problem.

      10 unexpected newline
    

    So I change the code to this, and it works.

    package main
    
    import "fmt"
    
    func main() {
        var num int
        for i := 0; i < 10; i++ {
            fmt.Printf("Debug: i : %d ", i)
            fmt.Scanf("%d
    ", &num) // add "
    "
            fmt.Println(num)
        }
    }
    

    this is because of the different line endings. the windows uses carriage return and line feed( ) as a line ending. the Unix uses the line feed( ).

    you can use notepad2 to create a file (a.txt) with line feed. and do this:

      go run s.go < input.txt
    

    this will work correctly.

    点赞 评论 复制链接分享
  • doubaoguo7469 doubaoguo7469 2013-03-08 03:46

    Just to point out fmt.Scanln(&num) would probably work the same as fmt.Scanf("%d ",&num), since fmt.Scanln(&num) also check the type of "num".

    In other words, if

    var num float32
    
    fmt.Scanln(&num)
    

    you can input floating number from the console.

    点赞 评论 复制链接分享

相关推荐