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

我正在阅读 Caleb Doxsey 的 Go 书,我有两个关于http://www.golang-book.com/4 的问题fmt.Scanf


我想知道为什么程序在第二次 Scanf 之后没有停止并等待用户输入?以及如何测试用户是否输入了整数和/或没有留空?


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))

}


杨魅力
浏览 232回答 1
1回答

回首忆惘然

这是发行5391:fmt:Scanf拒绝\r\n在Windows上线的末端。作为一种解决方法并检查有效输入,写入,var inputSquare float64n, err := fmt.Scanf("%f\n", &inputSquare)if err != nil || n != 1 {&nbsp; &nbsp; // handle invalid input&nbsp; &nbsp; fmt.Println(n, err)}和var inputGuess float64n, err = fmt.Scanf("%f\n", &inputGuess)if err != nil || n != 1 {&nbsp; &nbsp; // handle invalid input&nbsp; &nbsp; fmt.Println(n, err)}解决方法是"%f\n"格式字符串中的换行符。包裹 fmt功能扫描func Scanf(format string, a ...interface{}) (n int, err error)Scanf扫描从标准输入读取的文本,将连续的空格分隔值存储到由格式确定的连续参数中。它返回成功扫描的项目数。这是一个完整的工作程序:package mainimport (&nbsp; &nbsp; "fmt")// compute square roots by using Newton's methodfunc main() {&nbsp; &nbsp; var x float64 //number to take square root&nbsp; &nbsp; var y float64 //this is the guess&nbsp; &nbsp; var q float64 //this is the quotient&nbsp; &nbsp; var a float64 //this is the average&nbsp; &nbsp; fmt.Print("Enter a number to take its square root: ")&nbsp; &nbsp; var inputSquare float64&nbsp; &nbsp; n, err := fmt.Scanf("%f\n", &inputSquare)&nbsp; &nbsp; if err != nil || n != 1 {&nbsp; &nbsp; &nbsp; &nbsp; // handle invalid input&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(n, err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Print("Enter first guess ")&nbsp; &nbsp; var inputGuess float64&nbsp; &nbsp; n, err = fmt.Scanf("%f\n", &inputGuess)&nbsp; &nbsp; if err != nil || n != 1 {&nbsp; &nbsp; &nbsp; &nbsp; // handle invalid input&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(n, err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; x = inputSquare&nbsp; &nbsp; y = inputGuess&nbsp; &nbsp; for i := 0; i < 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; q = x / y&nbsp; &nbsp; &nbsp; &nbsp;// compute the quotient; x and y are given&nbsp; &nbsp; &nbsp; &nbsp; a = (q + y) / x // compute the average&nbsp; &nbsp; &nbsp; &nbsp; y = a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// set the guess to the average&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("sqrt(%g) = %g\n", x, y)}输出:Enter a number to take its square root: 2.0Enter first guess 1.0sqrt(2) = 1.414213562373095我在 Windows 7 上使用了 Go 1.1.1:C:\>go versiongo version go1.1.1 windows/amd64&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go