如何迭代打印多个值的过程?

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

How to iterate the process of printing multiple values?

问题

在这个Go语言代码中,我要求一个人输入他的名字。在输入名字后,数据将从values1变量中获取,并且结果将按照给定的键打印出来,就像这样:

给定结果

  1. 输入你的名字:samename
  2. 这是名字:samename
  3. 这是电子邮件地址:email one
  4. 这是工作角色:job one

我还想打印被注释的values2变量的数据。类似地,如果我有n个值,我需要重复使用for i := range values1吗?

所需结果

  1. 输入你的名字:samename
  2. 这是名字:samename
  3. 这是电子邮件地址:email one
  4. 这是工作角色:job role one
  5. 这是名字:samename
  6. 这是电子邮件地址:email two
  7. 这是工作角色:job role two
  8. ...
  9. ...

代码

  1. package main
  2. import "fmt"
  3. func main() {
  4. var name string
  5. keys := [3]string{"name", "email address", "job role"}
  6. values1 := [3]string{"samename", "email one", "job role one"}
  7. // values2 := [3]string{"samename", "email two", "job role two"}
  8. for {
  9. fmt.Printf("输入你的名字:")
  10. fmt.Scanln(&name)
  11. fmt.Println()
  12. if name == values1[0] {
  13. for i := range values1 {
  14. // j是keys的索引
  15. j := i % len(keys)
  16. // 在组之间打印空行。
  17. if j == 0 && i > 0 {
  18. fmt.Println()
  19. }
  20. fmt.Printf("这是%s:%s\n", keys[j], values1[i])
  21. }
  22. break
  23. } else {
  24. fmt.Println("名字未找到。请重试!")
  25. }
  26. }
  27. }
英文:

In this golang code, I am asking a person to enter his name. After entering the name, the data will be taken from the values1 variable, and as a result, they will be printed out with the given keys like this

Given result

  1. Enter your name: samename
  2. Here is the name: samename
  3. Here is the email address: email one
  4. Here is the job role: job one

I also want to print the data of the values2 variable that is commented. Similarly, if I have the n number of values, will I have to repeat the for i := range values1 again and again?

Required result

  1. Enter your name: samename
  2. Here is the name: samename
  3. Here is the email address: email one
  4. Here is the job role: job role one
  5. Here is the name: samename
  6. Here is the email address: email two
  7. Here is the job role: job role two
  8. ...
  9. ...

Code

  1. package main
  2. import "fmt"
  3. func main() {
  4. var name string
  5. keys := [3]string{"name", "email address", "job role"}
  6. values1 := [3]string{"samename", "email one", "job role one"}
  7. // values2 := [3]string{"samename", "email two", "job role two"}
  8. for {
  9. fmt.Printf("Enter your name: ")
  10. fmt.Scanln(&name)
  11. fmt.Println()
  12. if name == values1[0] {
  13. for i := range values1 {
  14. // j is index into keys
  15. j := i % len(keys)
  16. // Print blank line between groups.
  17. if j == 0 && i > 0 {
  18. fmt.Println()
  19. }
  20. fmt.Printf("Here is the %s: %s\n", keys[j], values1[i])
  21. }
  22. break
  23. } else {
  24. fmt.Println("Name is not found. Try again!")
  25. }
  26. }
  27. }

答案1

得分: 2

如果你需要打印键值对,可以使用map。使用map,你可以同时打印键和值。

  1. package main
  2. import "fmt"
  3. func main() {
  4. var name string
  5. value1 := map[string]string{"name": "samename", "email": "email one", "role": "job role one"}
  6. value2 := map[string]string{"name": "samename", "email": "email two", "role": "job role two"}
  7. values := []map[string]string{value1, value2}
  8. var found bool
  9. for {
  10. fmt.Printf("请输入你的名字:")
  11. fmt.Scanln(&name)
  12. fmt.Println()
  13. for _, value := range values {
  14. if value["name"] == name {
  15. found = true
  16. for k, v := range value {
  17. fmt.Printf("这是%s的值:%s\n", k, v)
  18. }
  19. fmt.Println()
  20. }
  21. }
  22. if found {
  23. break
  24. } else {
  25. fmt.Println("名字未找到,请重试!")
  26. }
  27. }
  28. }

希望对你有帮助!

英文:

if you need to print the keys, maybe you can use map. with map you can print both of key and value.

  1. package main
  2. import "fmt"
  3. func main() {
  4. var name string
  5. value1 := map[string]string{"name": "samename", "email": "email one", "role": "job role one"}
  6. value2 := map[string]string{"name": "samename", "email": "email two", "role": "job role two"}
  7. values := []map[string]string{value1, value2}
  8. var found bool
  9. for {
  10. fmt.Printf("Enter your name: ")
  11. fmt.Scanln(&name)
  12. fmt.Println()
  13. for _, value := range values {
  14. if value["name"] == name {
  15. found = true
  16. for k, v := range value {
  17. fmt.Printf("Here is the %s: %s\n", k, v)
  18. }
  19. fmt.Println()
  20. }
  21. }
  22. if found {
  23. break
  24. } else {
  25. fmt.Println("Name is not found. Try again!")
  26. }
  27. }
  28. }

huangapple
  • 本文由 发表于 2022年9月3日 15:28:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/73590440.html
匿名

发表评论

匿名网友

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

确定