访问包含在切片中的字符串。

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

Accessing strings contained in slice

问题

我正在做一些编程练习,以更好地理解Go语言。其中一个练习要求我创建一个程序,按照以下方式接受用户输入:

  • 第一行指定将作为输入提供的字符串数量
  • 随后的N行将分别是单个字符串

我需要输出每个字符串的偶数索引和奇数索引对应的字符,用空格分隔,并且每个字符串占一行。

示例输入:

  1. 2
  2. foo_bar
  3. fizz_buzz

应该输出:

  1. fobr o_a
  2. fz_uz izbz

但是在我的程序中,访问字符串切片返回了一个空字符串:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. // 读取一个整数,表示将输入多少个字符串
  7. var num_strings int
  8. fmt.Scan(&num_strings)
  9. // 创建一个字符串切片来保存输入的字符串
  10. strings := make([]string, num_strings)
  11. // 将输入的字符串添加到切片中
  12. for i := 0; i < num_strings; i++ {
  13. var temp string
  14. fmt.Scan(&temp)
  15. strings = append(strings, temp)
  16. }
  17. // 检查字符串是否已添加到切片中
  18. fmt.Println("Strings:", strings)
  19. // 检查是否可以访问字符串
  20. for i := 0; i < num_strings; i++ {
  21. fmt.Println(i, strings[i]) // 只有 i 打印出来了,而不是 strings[i]
  22. }
  23. // 遍历所有字符串
  24. for i := 0; i < num_strings; i++ {
  25. // 如果字符串索引是偶数,则打印字符
  26. for index, val := range strings[i] {
  27. if index%2 == 0 {
  28. fmt.Print(val)
  29. }
  30. }
  31. fmt.Print(" ")
  32. // 如果字符串索引是奇数,则打印字符
  33. for index, val := range strings[i] {
  34. if index%2 != 0 {
  35. fmt.Print(val)
  36. }
  37. }
  38. // 换行以处理下一个字符串
  39. fmt.Print("\n")
  40. }
  41. }
  1. 2
  2. foo_bar
  3. fizz_buzz
  4. Strings: [ foo_bar fizz_buzz]
  5. 0
  6. 1
英文:

I am working on some coding exercises to better understand Go. A given exercise instructs me to create a program that will accept user input as follows:

  • The first line specifies how many strings will be provided as input on separate lines
  • The subsequent N lines will each be single strings

I am to output the characters corresponding to even and odd indices of each string separated by a space, and each string on it's separate line.

Example Input:

  1. 2
  2. foo_bar
  3. fizz_buzz

Should Output:

  1. fobr o_a
  2. fz_uz izbz

But in my program accessing a slice of strings returns an empty string:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func main() {
  6. // read an integer describing how many strings will be input
  7. var num_strings int
  8. fmt.Scan(&amp;num_strings)
  9. // create a slice of strings to hold provided strings
  10. strings := make([]string, num_strings)
  11. // add provided strings to slice
  12. for i := 0; i &lt; num_strings; i++ {
  13. var temp string
  14. fmt.Scan(&amp;temp)
  15. strings = append(strings, temp)
  16. }
  17. // check that strings have been appended
  18. fmt.Println(&quot;Strings:&quot;, strings)
  19. // check that strings can be accessed
  20. for i := 0; i &lt; num_strings; i++ {
  21. fmt.Println(i, strings[i]) // only i prints, not strings[i]
  22. }
  23. // loop over all strings
  24. for i := 0; i &lt; num_strings; i++ {
  25. // if string index is even print the char
  26. for index, val := range strings[i] {
  27. if index%2 == 0 {
  28. fmt.Print(val)
  29. }
  30. }
  31. fmt.Print(&quot; &quot;)
  32. // if string index is odd print the char
  33. for index, val := range strings[i] {
  34. if index%2 != 0 {
  35. fmt.Print(val)
  36. }
  37. }
  38. // newline for next string
  39. fmt.Print(&quot;\n&quot;)
  40. }
  41. }
  1. 2
  2. foo_bar
  3. fizz_buzz
  4. Strings: [ foo_bar fizz_buzz]
  5. 0
  6. 1

答案1

得分: 2

因为当你使用make创建你的strings切片时,你创建了一个容量和长度都为n的切片。所以当你向其中添加元素时,你增加了切片的长度:

访问包含在切片中的字符串。

将这段代码修改为:

  1. // 创建一个用于保存提供的字符串的切片
  2. strings := []string{}
  3. // 向切片中添加提供的字符串
  4. for i := 0; i < num_strings; i++ {
  5. var temp string
  6. fmt.Scan(&temp)
  7. strings = append(strings, temp)
  8. }

或者

  1. // 创建一个用于保存提供的字符串的切片
  2. strings := make([]string, num_strings)
  3. // 向切片中添加提供的字符串
  4. for i := 0; i < num_strings; i++ {
  5. var temp string
  6. fmt.Scan(&temp)
  7. strings[i] = temp
  8. }

这样就可以了。

英文:

Because when you make your strings slice, you're creating a slice with both a capacity and length of n. So when you append to it, you're increasing the length of the slice:

访问包含在切片中的字符串。

Change this bit of code:

  1. // create a slice of strings to hold provided strings
  2. strings := make([]string, num_strings)
  3. // add provided strings to slice
  4. for i := 0; i &lt; num_strings; i++ {
  5. var temp string
  6. fmt.Scan(&amp;temp)
  7. strings = append(strings, temp)
  8. }

to either:

  1. // create a slice of strings to hold provided strings
  2. strings := []{}
  3. // add provided strings to slice
  4. for i := 0; i &lt; num_strings; i++ {
  5. var temp string
  6. fmt.Scan(&amp;temp)
  7. strings = append(strings, temp)
  8. }

Or

  1. // create a slice of strings to hold provided strings
  2. strings := make([]string, num_strings)
  3. // add provided strings to slice
  4. for i := 0; i &lt; num_strings; i++ {
  5. var temp string
  6. fmt.Scan(&amp;temp)
  7. strings[i] = temp
  8. }

And you should be good.

huangapple
  • 本文由 发表于 2022年12月17日 08:12:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/74830932.html
匿名

发表评论

匿名网友

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

确定