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

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

Accessing strings contained in slice

问题

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

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

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

示例输入:

2
foo_bar
fizz_buzz

应该输出:

fobr o_a
fz_uz izbz

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

package main

import (
	"fmt"
)

func main() {
	// 读取一个整数,表示将输入多少个字符串
	var num_strings int
	fmt.Scan(&num_strings)

	// 创建一个字符串切片来保存输入的字符串
	strings := make([]string, num_strings)

	// 将输入的字符串添加到切片中
	for i := 0; i < num_strings; i++ {
		var temp string
		fmt.Scan(&temp)
		strings = append(strings, temp)
	}

	// 检查字符串是否已添加到切片中
	fmt.Println("Strings:", strings)

	// 检查是否可以访问字符串
	for i := 0; i < num_strings; i++ {
		fmt.Println(i, strings[i]) // 只有 i 打印出来了,而不是 strings[i]
}

	// 遍历所有字符串
	for i := 0; i < num_strings; i++ {

		// 如果字符串索引是偶数,则打印字符
		for index, val := range strings[i] {
			if index%2 == 0 {
				fmt.Print(val)
			}
		}

		fmt.Print(" ")

		// 如果字符串索引是奇数,则打印字符
		for index, val := range strings[i] {
			if index%2 != 0 {
				fmt.Print(val)
			}
		}

		// 换行以处理下一个字符串
		fmt.Print("\n")
	}
}
2
foo_bar
fizz_buzz
Strings: [  foo_bar fizz_buzz]
0 
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:

2
foo_bar
fizz_buzz

Should Output:

fobr o_a
fz_uz izbz

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

package main

import (
	&quot;fmt&quot;
)

func main() {
	// read an integer describing how many strings will be input
	var num_strings int
	fmt.Scan(&amp;num_strings)

	// create a slice of strings to hold provided strings
	strings := make([]string, num_strings)

	// add provided strings to slice
	for i := 0; i &lt; num_strings; i++ {
		var temp string
		fmt.Scan(&amp;temp)
		strings = append(strings, temp)
	}

	// check that strings have been appended
	fmt.Println(&quot;Strings:&quot;, strings)

	// check that strings can be accessed
	for i := 0; i &lt; num_strings; i++ {
		fmt.Println(i, strings[i]) // only i prints, not strings[i]
	}

	// loop over all strings
	for i := 0; i &lt; num_strings; i++ {

		// if string index is even print the char
		for index, val := range strings[i] {
			if index%2 == 0 {
				fmt.Print(val)
			}
		}

		fmt.Print(&quot; &quot;)

		// if string index is odd print the char
		for index, val := range strings[i] {
			if index%2 != 0 {
				fmt.Print(val)
			}
		}

		// newline for next string
		fmt.Print(&quot;\n&quot;)
	}
}

2
foo_bar
fizz_buzz
Strings: [  foo_bar fizz_buzz]
0 
1 
 
 

答案1

得分: 2

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

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

将这段代码修改为:

// 创建一个用于保存提供的字符串的切片
strings := []string{}

// 向切片中添加提供的字符串
for i := 0; i < num_strings; i++ {
  var temp string
  fmt.Scan(&temp)
  strings = append(strings, temp)
}

或者

// 创建一个用于保存提供的字符串的切片
strings := make([]string, num_strings)

// 向切片中添加提供的字符串
for i := 0; i < num_strings; i++ {
  var temp string
  fmt.Scan(&temp)
  strings[i] = temp
}

这样就可以了。

英文:

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:

// create a slice of strings to hold provided strings
strings := make([]string, num_strings)

// add provided strings to slice
for i := 0; i &lt; num_strings; i++ {
  var temp string
  fmt.Scan(&amp;temp)
  strings = append(strings, temp)
}

to either:

// create a slice of strings to hold provided strings
strings := []{}

// add provided strings to slice
for i := 0; i &lt; num_strings; i++ {
  var temp string
  fmt.Scan(&amp;temp)
  strings = append(strings, temp)
}

Or

// create a slice of strings to hold provided strings
strings := make([]string, num_strings)

// add provided strings to slice
for i := 0; i &lt; num_strings; i++ {
  var temp string
  fmt.Scan(&amp;temp)
  strings[i] = temp
}

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:

确定