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

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

strings.Split, stdin not returning slice/array

问题

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

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var seedCost float64
	var lawnNo int
	var in string
	var area float64
	var wh []string

	fmt.Scanln(&seedCost) // 输入2
	fmt.Scanln(&lawnNo)   // 输入3
	lawnCost := 0.00

	for i := 0; i < lawnNo; i++ {
		fmt.Scanln(&in) // 输入2 3
		wh = strings.Split(in, " ")
		fmt.Println(wh[0])
		fmt.Println(wh[1]) // 测试抛出异常,索引超出范围
		w, _ := strconv.ParseFloat(wh[0], 64)
		h, _ := strconv.ParseFloat(wh[1], 64) // 异常行,索引超出范围

		area = w * h
		lawnCost += area * seedCost
	}

	ans := strconv.FormatFloat(lawnCost, 'E', 8, 64)
	fmt.Println(ans)
}
英文:

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.

package main

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

func main() {
    var seedCost float64
    var lawnNo int
    var in string
    var area float64
    var wh []string

    fmt.Scanln(&amp;seedCost) //2 is inputted
    fmt.Scanln(&amp;lawnNo)   //3
    lawnCost := 0.00

    for i := 0; i &lt; lawnNo; i++ {
    	fmt.Scanln(&amp;in) //2 3 is inputted
    	wh = strings.Split(in, &quot; &quot;)
    	fmt.Println(wh[0])
	    fmt.Println(wh[1]) //Test throwing exception, index out of range
    	w, _ := strconv.ParseFloat(wh[0], 64)
	    h, _ := strconv.ParseFloat(wh[1], 64) //EXCEPTIONAL LINE, index out of range

    	area = w * h
    	lawnCost += area * seedCost
    }

    ans := strconv.FormatFloat(lawnCost, &#39;E&#39;, 8, 64)
    fmt.Println(ans)
}

答案1

得分: 1

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

尝试使用bufio包:

scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
in := scanner.Text()
wh := strings.Split(in, " ")
fmt.Println(wh[0])
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:

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

答案2

得分: 0

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

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

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

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

答案3

得分: 0

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

解决这个问题的两种方法

第一种方法:使用Scanf

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

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

第二种方法:使用bufio

我认为这是最好的方法

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

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

英文:

fmt.Scanln stops reading after the spaces.

Two Approaches to solve this Problem

1st Approach: USE Scanf

var in string
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

scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan(){
   in = scanner.Text()
   break  //optional line if your input has a single line
}
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:

确定