英文:
How to iterate the process of printing multiple values?
问题
在这个Go语言代码中,我要求一个人输入他的名字。在输入名字后,数据将从values1
变量中获取,并且结果将按照给定的键打印出来,就像这样:
给定结果
输入你的名字:samename
这是名字:samename
这是电子邮件地址:email one
这是工作角色:job one
我还想打印被注释的values2
变量的数据。类似地,如果我有n
个值,我需要重复使用for i := range values1
吗?
所需结果
输入你的名字:samename
这是名字:samename
这是电子邮件地址:email one
这是工作角色:job role one
这是名字:samename
这是电子邮件地址:email two
这是工作角色:job role two
...
...
代码
package main
import "fmt"
func main() {
var name string
keys := [3]string{"name", "email address", "job role"}
values1 := [3]string{"samename", "email one", "job role one"}
// values2 := [3]string{"samename", "email two", "job role two"}
for {
fmt.Printf("输入你的名字:")
fmt.Scanln(&name)
fmt.Println()
if name == values1[0] {
for i := range values1 {
// j是keys的索引
j := i % len(keys)
// 在组之间打印空行。
if j == 0 && i > 0 {
fmt.Println()
}
fmt.Printf("这是%s:%s\n", keys[j], values1[i])
}
break
} else {
fmt.Println("名字未找到。请重试!")
}
}
}
英文:
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
Enter your name: samename
Here is the name: samename
Here is the email address: email one
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
Enter your name: samename
Here is the name: samename
Here is the email address: email one
Here is the job role: job role one
Here is the name: samename
Here is the email address: email two
Here is the job role: job role two
...
...
Code
package main
import "fmt"
func main() {
var name string
keys := [3]string{"name", "email address", "job role"}
values1 := [3]string{"samename", "email one", "job role one"}
// values2 := [3]string{"samename", "email two", "job role two"}
for {
fmt.Printf("Enter your name: ")
fmt.Scanln(&name)
fmt.Println()
if name == values1[0] {
for i := range values1 {
// j is index into keys
j := i % len(keys)
// Print blank line between groups.
if j == 0 && i > 0 {
fmt.Println()
}
fmt.Printf("Here is the %s: %s\n", keys[j], values1[i])
}
break
} else {
fmt.Println("Name is not found. Try again!")
}
}
}
答案1
得分: 2
如果你需要打印键值对,可以使用map
。使用map
,你可以同时打印键和值。
package main
import "fmt"
func main() {
var name string
value1 := map[string]string{"name": "samename", "email": "email one", "role": "job role one"}
value2 := map[string]string{"name": "samename", "email": "email two", "role": "job role two"}
values := []map[string]string{value1, value2}
var found bool
for {
fmt.Printf("请输入你的名字:")
fmt.Scanln(&name)
fmt.Println()
for _, value := range values {
if value["name"] == name {
found = true
for k, v := range value {
fmt.Printf("这是%s的值:%s\n", k, v)
}
fmt.Println()
}
}
if found {
break
} else {
fmt.Println("名字未找到,请重试!")
}
}
}
希望对你有帮助!
英文:
if you need to print the keys, maybe you can use map
. with map
you can print both of key and value.
package main
import "fmt"
func main() {
var name string
value1 := map[string]string{"name": "samename", "email": "email one", "role": "job role one"}
value2 := map[string]string{"name": "samename", "email": "email two", "role": "job role two"}
values := []map[string]string{value1, value2}
var found bool
for {
fmt.Printf("Enter your name: ")
fmt.Scanln(&name)
fmt.Println()
for _, value := range values {
if value["name"] == name {
found = true
for k, v := range value {
fmt.Printf("Here is the %s: %s\n", k, v)
}
fmt.Println()
}
}
if found {
break
} else {
fmt.Println("Name is not found. Try again!")
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论