检查用户输入字符串

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

Checking user input string

问题

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

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

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "math"
  8. "strings"
  9. )
  10. func prompt(toprint string) string{
  11. if(toprint == ""){
  12. toprint = "请输入文本:";
  13. }
  14. reader := bufio.NewReader(os.Stdin);
  15. fmt.Println(toprint);
  16. text, _ := reader.ReadString('\n');
  17. return text;
  18. }
  19. func main() {
  20. choice := prompt("请输入'1'");
  21. if(strings.Compare("1",choice)==0||choice=="1"){
  22. // 即使choice=="1",也不会进入这里
  23. }else{
  24. // 总是进入这里
  25. }
  26. }

谢谢你的帮助。

英文:

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.

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "math"
  8. "strings"
  9. )
  10. func prompt(toprint string) string{
  11. if(toprint == ""){
  12. toprint = "Enter text :";
  13. }
  14. reader := bufio.NewReader(os.Stdin);
  15. fmt.Println(toprint);
  16. text, _ := reader.ReadString('\n');
  17. return text;
  18. }
  19. func main() {
  20. choice := prompt("Please enter '1'");
  21. if(strings.Compare("1",choice)==0||choice=="1"){
  22. // D'ONT ENTER HERE EVEN WHEN choice=="1"
  23. }else{
  24. // Always go here
  25. }
  26. }

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()的最后加上

  1. 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

  1. return strings.TrimSpace(text)

at the end of prompt().

答案2

得分: 0

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

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

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

英文:

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

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

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:

确定