检查用户输入字符串

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

Checking user input string

问题

我是你的中文翻译助手,以下是你提供的代码的翻译:

我是GoLang的新手,遇到了一个问题:
即使用户输入是"1",它也不会进入if语句。

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"math"
	"strings"
)

func prompt(toprint string) string{
	if(toprint == ""){
		toprint = "请输入文本:";
	}
	reader := bufio.NewReader(os.Stdin);
	fmt.Println(toprint);
	text, _ := reader.ReadString('\n');
	return text;
}

func main() {
	choice := prompt("请输入'1'");

	if(strings.Compare("1",choice)==0||choice=="1"){
		// 即使choice=="1",也不会进入这里
	}else{
		// 总是进入这里
	}
}

谢谢你的帮助。

英文:

I am new in GoLang and I am encountering a problem with this condition:
Even if the input of the user is "1", it doesn't enter in the if statement.

package main

import (
    "bufio"
    "fmt"
    "os"
	"strconv"
	"math"
	"strings"
)

func prompt(toprint string) string{
	if(toprint == ""){
		toprint = "Enter text :";
	}
	reader := bufio.NewReader(os.Stdin);
	fmt.Println(toprint);
	text, _ := reader.ReadString('\n');
	return text;
}

func main() {
	choice := prompt("Please enter '1'");

	if(strings.Compare("1",choice)==0||choice=="1"){
		// D'ONT ENTER HERE EVEN WHEN choice=="1"
	}else{
		// Always go here
	}
}

Thank you for your help.

答案1

得分: 4

这是因为reader.ReadString返回的文本包括分隔符,所以返回的字符串将是1\n而不仅仅是1。根据文档(我强调):

> func (*Reader) ReadString
>
> func (b *Reader) ReadString(delim byte) (string, error)
>
> ReadString会读取输入中第一个出现的delim之前的内容,并返回一个包含数据的字符串,包括分隔符。如果在找到分隔符之前遇到错误,ReadString会返回错误之前读取的数据和错误本身(通常是io.EOF)。只有当返回的数据不以delim结尾时,ReadString才会返回err != nil。对于简单的用法,使用Scanner可能更方便。

也许你想在prompt()的最后加上

return strings.TrimSpace(text)

来去除空白字符。

英文:

This is because reader.ReadString returns all the text including the delimiter, so the string returned will be 1\n not just 1. From the documentation (my emphasis):

> func (*Reader) ReadString
>
> func (b *Reader) ReadString(delim byte) (string, error)
>
> ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadString returns err != nil if and only if the returned data does not end in delim. For simple uses, a Scanner may be more convenient.

Perhaps you want to do

return strings.TrimSpace(text)

at the end of prompt().

答案2

得分: 0

谢谢!这是一个返回正确输入的 "prompt()" 代码:

func prompt(toprint string) string{
    if(toprint == ""){
        toprint = "请输入文本:";
    }
    reader := bufio.NewReader(os.Stdin);
    fmt.Println(toprint);
    text, _ := reader.ReadString('\n');
    return text[0:len(text)-2];
}

请注意,我已经将代码中的注释翻译为中文。

英文:

Thank you !
Here's the "prompt()" code which returns the correct input :

func prompt(toprint string) string{
	if(toprint == ""){
		toprint = "Enter text :";
	}
	reader := bufio.NewReader(os.Stdin);
	fmt.Println(toprint);
	text, _ := reader.ReadString('\n');
	return text[0:len(text)-2];
}

huangapple
  • 本文由 发表于 2017年1月22日 02:45:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/41783108.html
匿名

发表评论

匿名网友

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

确定