英文:
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 (
"fmt"
"strconv"
"strings"
)
func main() {
var seedCost float64
var lawnNo int
var in string
var area float64
var wh []string
fmt.Scanln(&seedCost) //2 is inputted
fmt.Scanln(&lawnNo) //3
lawnCost := 0.00
for i := 0; i < lawnNo; i++ {
fmt.Scanln(&in) //2 3 is inputted
wh = strings.Split(in, " ")
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, 'E', 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(&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, " ")
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(&in, &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("%q", &in)
But then your input should be enclosed within the double quotes like "2 3"
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论