这段 Go 代码为什么会忽略第二个输入的数字?

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

Why does this go code ignore the second inputed number?

问题

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Println("输入字符串=>")
	a1, _ := reader.ReadString('\n')
	a1 = strings.Replace(a1, "\n", "", -1)
	b1 := strings.Split(a1, " ")
	var b2 [2]int
	for i := 0; i < 2; i++ {
		b2[i], _ = strconv.Atoi(b1[i])
	}
	fmt.Println(b2[0] + b2[1])
}

这段代码产生以下输出:

输入字符串=> 5 6

11

但在JetBrains Goland中,它产生了正确的输出。

英文:
package main

import (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;strconv&quot;
	&quot;strings&quot;
)

func main()  {
	reader := bufio.NewReader(os.Stdin)
	fmt.Println(&quot;Enter String=&gt;&quot;)
	a1, _ := reader.ReadString(&#39;\n&#39;)
	a1 = strings.Replace(a1, &quot;\n&quot;, &quot;&quot;, -1)
	b1 := strings.Split(a1, &quot; &quot;)
	var b2 [2]int
	for i := 0; i &lt; 2 ; i++ {
		b2[i], _ = strconv.Atoi(b1[i])
	}
	fmt.Println(b2[0] + b2[1])
}

This code produces the following output

Enter String=&gt; 5 6

5

But in Jetbrains Goland it produces the correct output.

答案1

得分: 2

Windows使用CRLF (\r\n)作为换行符。使用strings.TrimSpace而不是Replace

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Println("输入字符串=>")
    a1, _ := reader.ReadString('\n')
    a1 = strings.TrimSpace(a1)
    b1 := strings.Split(a1, " ")
    var b2 [2]int
    for i := 0; i < 2; i++ {
        b2[i], _ = strconv.Atoi(b1[i])
    }
    fmt.Println(b2[0] + b2[1])
}

或者,使用strings.Fields代替Replace和Split

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Println("输入字符串=>")
    a1, _ := reader.ReadString('\n')
    b1 := strings.Fields(a1)
    var b2 [2]int
    for i := 0; i < 2; i++ {
        b2[i], _ = strconv.Atoi(b1[i])
    }
    fmt.Println(b2[0] + b2[1])
}
英文:

Windows terminates lines with CRLF (\r\n). Use strings.TrimSpace, not Replace.

package main

import (
    &quot;bufio&quot;
    &quot;fmt&quot;
    &quot;os&quot;
    &quot;strconv&quot;
    &quot;strings&quot;
)

func main()  {
    reader := bufio.NewReader(os.Stdin)
    fmt.Println(&quot;Enter String=&gt;&quot;)
    a1, _ := reader.ReadString(&#39;\n&#39;)
    a1 = strings.TrimSpace(a1)
    b1 := strings.Split(a1, &quot; &quot;)
    var b2 [2]int
    for i := 0; i &lt; 2 ; i++ {
        b2[i], _ = strconv.Atoi(b1[i])
    }
    fmt.Println(b2[0] + b2[1])
}

Or, use strings.Fields instead of Replace and Split.

package main

import (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;strconv&quot;
	&quot;strings&quot;
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Println(&quot;Enter String=&gt;&quot;)
	a1, _ := reader.ReadString(&#39;\n&#39;)
	b1 := strings.Fields(a1)
	var b2 [2]int
	for i := 0; i &lt; 2; i++ {
		b2[i], _ = strconv.Atoi(b1[i])
	}
	fmt.Println(b2[0] + b2[1])
}

huangapple
  • 本文由 发表于 2021年9月25日 16:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/69324237.html
匿名

发表评论

匿名网友

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

确定