利用地图数据进行分析
时间:2024-03-11
浏览:372
作者:佚名
有志者,事竟成!如果你在学习Golang,那么本文《利用地图数据进行分析》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
问题内容
我想使用我创建的地图中的值来乘以输入给出的天数。
我只是不知道如何扫描存储在地图中的值。
package main
import "fmt"
func main() {
typeCar := map[string]int{
"audi": 50,
"volvo": 100,
"tesla": 300,
}
fmt.Print("how many days would you like to rent?: ")
var days int
fmt.Scanf("%d", &days)
fmt.Println("the price is:", typeCar["audi"]*days, "euro")
// "typeCar["audi"]" should be the input of the user instead.
}解决方案
您可以获取字符串形式的用户输入,并根据地图对其进行测试以检索关联的值。
package main
import "fmt"
func main() {
typeCar := map[string]int{
"audi": 50,
"volvo": 100,
"tesla": 300,
}
fmt.Print("how many days would you like to rent?: ")
var days int
fmt.Scanf("%d", &days)
// "typeCar["audi"]" should be the input of the user instead.
fmt.Printf("waht type %v ? ", typeCar)
var userInput string
fmt.Scan(&userInput)
tCar, ok := typeCar[userInput]
if !ok {
panic("not a valid car")
}
fmt.Println("the price is:", tCar*days, "euro")
}以上就是《利用地图数据进行分析》的详细内容,更多关于的资料请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
