dongyinting3179 2013-07-01 09:59
浏览 373
已采纳

为什么Go中的fmt.Scanf不等待用户输入?

I am working through Caleb Doxsey's Go book and I have two questions about fmt.Scanf http://www.golang-book.com/4

I am wondering why the program does not stop after the second Scanf and wait for user input? And how do I test if the user entered an integer and/or did not leave blank?

package main

import (
"fmt"
//"math"
)


// compute square roots by using Newton's method

func main() {

var x float64           //number to take square root
var y float64           //this is the guess
var q float64           //this is the quotient
var a float64           //this is the average


// how do check if the user entered a number
fmt.Print("Enter a number to take its square root: ")
var inputSquare float64
fmt.Scanf("%f", &inputSquare)

// why doesn't program stop after 
// the Print statement and wait
// for user input?
fmt.Print("Enter first guess ")
var inputGuess float64
fmt.Scanf("%f", &inputGuess)

//x = 2
x = inputSquare
y = inputGuess

for i := 0; i < 10; i++ {   //set up the for loop for iterations
    q = x/y                 //compute the quotient; x and y are given
    a = (q + y) / x         //compute the average       
    y = a                   //set the guess to the average              
}                           //for the next loop


fmt.Println("y --> ", y)
//fmt.Println("Sqrt(2)", math.Sqrt(2))
}
  • 写回答

1条回答 默认 最新

  • drrog9853 2013-07-01 11:48
    关注

    It's Issue 5391: fmt: Scanf rejects at end of line on Windows.

    As a workaround and to check for valid input, write,

    var inputSquare float64
    n, err := fmt.Scanf("%f
    ", &inputSquare)
    if err != nil || n != 1 {
        // handle invalid input
        fmt.Println(n, err)
    }
    

    and

    var inputGuess float64
    n, err = fmt.Scanf("%f
    ", &inputGuess)
    if err != nil || n != 1 {
        // handle invalid input
        fmt.Println(n, err)
    }
    

    The workaround is the newline in the "%f " format strings.

    Package fmt

    func Scanf

    func Scanf(format string, a ...interface{}) (n int, err error)
    

    Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned.

    Here's a complete working program:

    package main
    
    import (
        "fmt"
    )
    
    // compute square roots by using Newton's method
    func main() {
        var x float64 //number to take square root
        var y float64 //this is the guess
        var q float64 //this is the quotient
        var a float64 //this is the average
    
        fmt.Print("Enter a number to take its square root: ")
        var inputSquare float64
        n, err := fmt.Scanf("%f
    ", &inputSquare)
        if err != nil || n != 1 {
            // handle invalid input
            fmt.Println(n, err)
            return
        }
    
        fmt.Print("Enter first guess ")
        var inputGuess float64
        n, err = fmt.Scanf("%f
    ", &inputGuess)
        if err != nil || n != 1 {
            // handle invalid input
            fmt.Println(n, err)
            return
        }
    
        x = inputSquare
        y = inputGuess
        for i := 0; i < 10; i++ {
            q = x / y       // compute the quotient; x and y are given
            a = (q + y) / x // compute the average
            y = a           // set the guess to the average
        }
        fmt.Printf("sqrt(%g) = %g
    ", x, y)
    }
    

    Output:

    Enter a number to take its square root: 2.0
    Enter first guess 1.0
    sqrt(2) = 1.414213562373095
    

    I used Go 1.1.1 on Windows 7:

    C:\>go version
    go version go1.1.1 windows/amd64  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题