为什么Go中的fmt.Scanf不等待用户输入? [英] Why doesn't fmt.Scanf in Go wait for user input?

查看:341
本文介绍了为什么Go中的fmt.Scanf不等待用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Caleb Doxsey的Go书,我对fmt.Scanf有两个问题. http://www .golang-book.com/4

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

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

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

推荐答案

它是问题5391 :fmt:Scanf在Windows的行尾拒绝\r\n.

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

作为一种解决方法,要检查输入是否有效,请写

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

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

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

解决方法是使用"%f\n"格式字符串的换行符.

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

包装fmt

func Scanf

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

Scanf扫描从标准输入读取的文本,并连续存储 将空格分隔的值转换为由参数确定的连续参数 格式.它返回成功扫描的项目数.

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\n", &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\n", &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\n", x, y)
}

输出:

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

我在Windows 7上使用了Go 1.1.1:

I used Go 1.1.1 on Windows 7:

C:\>go version
go version go1.1.1 windows/amd64  

这篇关于为什么Go中的fmt.Scanf不等待用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆