遍历一个切片

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

Ranging over a slice

问题

我的问题是,我只被允许使用PrintRune命令,我必须遍历一个字符串,并逐个打印出任何字符串的字符。

  1. package piscine
  2. import "github.com/01-edu/z01"
  3. func PrintStr(s string) {
  4. slice := []string{
  5. s,
  6. }
  7. for x, word := range slice {
  8. z01.PrintRune(rune(word[x]))
  9. }
  10. }

这是我的代码,它只打印字符串的第一个字符,我该如何使切片继续直到给定字符串的末尾?

英文:

my problem is that Im only allowed to use the command PrintRune, i must range over a string and print one by one the characters of any string

  1. package piscine
  2. import "github.com/01-edu/z01"
  3. func PrintStr(s string) {
  4. slice := []string{
  5. s,
  6. }
  7. for x, word := range slice {
  8. z01.PrintRune(rune(word[x]))
  9. }
  10. }

here's my code, this only prints the first character of the string, how can i make the slice continue until the end of the given string please ?

答案1

得分: 1

这是代码片段:

  1. package piscine
  2. import "github.com/01-edu/z01"
  3. func PrintStr(s string) {
  4. slice := []string{
  5. s,
  6. }
  7. for _, word := range slice {
  8. for _, r := range word {
  9. z01.PrintRune(rune(r))
  10. }
  11. }
  12. }
英文:

Here is the code snippets:

  1. package piscine
  2. import "github.com/01-edu/z01"
  3. func PrintStr(s string) {
  4. slice := []string{
  5. s,
  6. }
  7. for _, word := range slice {
  8. for _, r := range word {
  9. z01.PrintRune(rune(r))
  10. }
  11. }
  12. }

huangapple
  • 本文由 发表于 2022年3月24日 18:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/71600985.html
匿名

发表评论

匿名网友

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

确定