在这段 golang 代码中,我要求一个人输入他的名字。输入名称后,数据将从values1变量中获取,结果,它们将像这样使用给定的键打印出来
给定结果
Enter your name: samename
Here is the name: samename
Here is the email address: email one
Here is the job role: job one
我还想打印values2被注释的变量的数据。同样,如果我有值的数量,我是否必须一次又一次n地重复?for i := range values1
要求的结果
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
...
...
代码
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!")
}
}
}
紫衣仙女
相关分类