strings.Split,stdin不返回切片/数组。

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

strings.Split, stdin not returning slice/array

问题

似乎strings.Split(" ")没有返回一个数组。我不知道为什么会这样。我确定我在其他地方以类似的上下文使用它。

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. func main() {
  8. var seedCost float64
  9. var lawnNo int
  10. var in string
  11. var area float64
  12. var wh []string
  13. fmt.Scanln(&seedCost) // 输入2
  14. fmt.Scanln(&lawnNo) // 输入3
  15. lawnCost := 0.00
  16. for i := 0; i < lawnNo; i++ {
  17. fmt.Scanln(&in) // 输入2 3
  18. wh = strings.Split(in, " ")
  19. fmt.Println(wh[0])
  20. fmt.Println(wh[1]) // 测试抛出异常,索引超出范围
  21. w, _ := strconv.ParseFloat(wh[0], 64)
  22. h, _ := strconv.ParseFloat(wh[1], 64) // 异常行,索引超出范围
  23. area = w * h
  24. lawnCost += area * seedCost
  25. }
  26. ans := strconv.FormatFloat(lawnCost, 'E', 8, 64)
  27. fmt.Println(ans)
  28. }
英文:

It seems that strings.split(" ") is not returning an array. I have no idea why this is. I'm sure I am using it in a similar context elsewhere.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strconv&quot;
  5. &quot;strings&quot;
  6. )
  7. func main() {
  8. var seedCost float64
  9. var lawnNo int
  10. var in string
  11. var area float64
  12. var wh []string
  13. fmt.Scanln(&amp;seedCost) //2 is inputted
  14. fmt.Scanln(&amp;lawnNo) //3
  15. lawnCost := 0.00
  16. for i := 0; i &lt; lawnNo; i++ {
  17. fmt.Scanln(&amp;in) //2 3 is inputted
  18. wh = strings.Split(in, &quot; &quot;)
  19. fmt.Println(wh[0])
  20. fmt.Println(wh[1]) //Test throwing exception, index out of range
  21. w, _ := strconv.ParseFloat(wh[0], 64)
  22. h, _ := strconv.ParseFloat(wh[1], 64) //EXCEPTIONAL LINE, index out of range
  23. area = w * h
  24. lawnCost += area * seedCost
  25. }
  26. ans := strconv.FormatFloat(lawnCost, &#39;E&#39;, 8, 64)
  27. fmt.Println(ans)
  28. }

答案1

得分: 1

这是因为fmt.Scanln在第一个空格后面不接受输入,所以如果你在fmt.Scanln(&in)中输入了2 3 4,那么只有2会被赋值给in。

尝试使用bufio包:

  1. scanner := bufio.NewScanner(os.Stdin)
  2. scanner.Scan()
  3. in := scanner.Text()
  4. wh := strings.Split(in, " ")
  5. fmt.Println(wh[0])
  6. fmt.Println(wh[1])
英文:

It is because fmt.Scanln doesn't take inputs after first space so if you entered 2 3 4 at fmt.Scanln(&amp;in) then only 2 will be assigned to in.

Try using package bufio:

  1. scanner := bufio.NewScanner(os.Stdin)
  2. scanner.Scan()
  3. in := scanner.Text()
  4. wh = strings.Split(in, &quot; &quot;)
  5. fmt.Println(wh[0])
  6. fmt.Println(wh[1])

答案2

得分: 0

Scanln似乎只能读取到空格之前的内容,所以我通过使用额外的字符串解决了这个问题。

  1. fmt.Scanln(&in, &in1)
英文:

Scanln seemed to only read until a space so I solved this problem by using an extra string

  1. fmt.Scanln(&amp;in, &amp;in1)

答案3

得分: 0

fmt.Scanln在空格后停止读取。

解决这个问题的两种方法

第一种方法:使用Scanf

  1. var in string
  2. fmt.Scanf("%q", &in)

但是你的输入应该用双引号括起来,例如"2 3"

第二种方法:使用bufio

我认为这是最好的方法

  1. scanner := bufio.NewScanner(os.Stdin)
  2. for scanner.Scan(){
  3. in = scanner.Text()
  4. break //如果你的输入只有一行,这行是可选的
  5. }
  6. fmt.Println(in)

这样可以以最佳的方式解决这个问题。

英文:

fmt.Scanln stops reading after the spaces.

Two Approaches to solve this Problem

1st Approach: USE Scanf

  1. var in string
  2. fmt.Scanf(&quot;%q&quot;, &amp;in)

But then your input should be enclosed within the double quotes like &quot;2 3&quot;

2nd Approach: USE bufio

The best way I consider

  1. scanner := bufio.NewScanner(os.Stdin)
  2. for scanner.Scan(){
  3. in = scanner.Text()
  4. break //optional line if your input has a single line
  5. }
  6. fmt.Println(in)

This shall solve the problem in the best way

huangapple
  • 本文由 发表于 2017年4月15日 08:55:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/43421062.html
匿名

发表评论

匿名网友

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

确定