猿问

在 golang 中显示一周的第二天

我刚开始学习 IT,我认为制作一个小程序来告诉你明天是什么日子会很有趣。可悲的是我被困住了。目前,当您从数组中写入正确的数字时它可以工作,但我希望它可以使用字符串。因此,当您输入“Maandag”(荷兰语为星期一)时,程序将回答 Dinsdag(荷兰语为星期二)


到目前为止,这是我的代码:


package main


import (

    "fmt"

)


func main() {


    var counter int


    var dag [7]string

    dag[0] = "Zondag"

    dag[1] = "Maandag"

    dag[2] = "Dinsdag"

    dag[3] = "Woensdag"

    dag[4] = "Donderdag"

    dag[5] = "Vrijdag"

    dag[6] = "Zaterdag"


    fmt.Println("Welke dag is het?")

    fmt.Scan(&counter)


    if counter == 6 {

        counter = 0

        fmt.Println(dag[counter])

    }


    if counter != 6 {

        counter++

        fmt.Println(dag[counter])

    }

}


慕森卡
浏览 120回答 3
3回答

慕丝7291255

遍历天数数组以获取其索引值。检查传递的字符串并获取匹配字符串索引的值。然后使用该索引获取第二天的值:package mainimport (    "fmt")var (    counter int    day     string)func main() {    var dag [7]string    dag[0] = "Zondag"    dag[1] = "Maandag"    dag[2] = "Dinsdag"    dag[3] = "Woensdag"    dag[4] = "Donderdag"    dag[5] = "Vrijdag"    dag[6] = "Zaterdag"    fmt.Println("Welke dag is het?")    fmt.Scan(&day)    for key, value := range dag {        if day == value {            counter = key        }    }    fmt.Println(counter)    if counter == 6 {        counter = 0        fmt.Println(dag[counter])    }    if counter != 6 {        counter = counter + 1        fmt.Println(dag[counter])    }}游乐场示例或者正如 Peter 所建议的那样,您也可以使用地图,在这种情况下更方便和易于使用:package mainimport (    "fmt")func main() {    var value string    dag := make(map[string]string)    dag["Zondag"] = "Maandag"    dag["Maandag"] = "Dinsdag"    dag["Dinsdag"] = "Woensdag"    dag["Woensdag"] = "Donderdag"    dag["Donderdag"] = "Vrijdag"    dag["Vrijdag"] = "Zaterdag"    fmt.Println("Welke dag is het?")    fmt.Scan(&value)    fmt.Println(dag[value])}Go Playground上的工作代码

心有法竹

你在找什么是枚举。在 Go 中,它们可以这样实现:type Weekday int&nbsp;const (&nbsp; &nbsp;Sunday&nbsp; &nbsp; Weekday = iota&nbsp; &nbsp;Monday&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Tuesday&nbsp; &nbsp;&nbsp; &nbsp;Wednesday&nbsp;&nbsp; &nbsp;Thursday&nbsp;&nbsp;&nbsp; &nbsp;Friday&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Saturday&nbsp; &nbsp;)func (day Weekday) String() string {&nbsp; &nbsp; // declare an array of strings&nbsp; &nbsp; // ... operator counts how many&nbsp; &nbsp; // items in the array (7)&nbsp; &nbsp; names := [...]string{&nbsp; &nbsp; &nbsp; &nbsp; "Sunday",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Monday",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Tuesday",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Wednesday",&nbsp; &nbsp; &nbsp; &nbsp; "Thursday",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Friday",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Saturday"}&nbsp; &nbsp; // → `day`: It's one of the&nbsp; &nbsp; // values of Weekday constants.&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // If the constant is Sunday,&nbsp; &nbsp; // then day is 0.&nbsp; &nbsp; // prevent panicking in case of&nbsp; &nbsp; // `day` is out of range of Weekday&nbsp; &nbsp; if day < Sunday || day > Saturday {&nbsp; &nbsp; &nbsp; return "Unknown"&nbsp; &nbsp; }&nbsp; &nbsp; // return the name of a Weekday&nbsp; &nbsp; // constant from the names array&nbsp;&nbsp; &nbsp; // above.&nbsp; &nbsp; return names[day]}// will display "Sunday"fmt.Println(Sunday)// will display "Monday"fmt.Println(Sunday + 1)如果你不需要 int 底层类型,你可以这样创建它:const (&nbsp; &nbsp; Sunday = "Sunday"&nbsp; &nbsp; &nbsp;//...)

qq_花开花谢_0

您可以枚举数组并传输数字和字符串func GetNextDay(someday string){&nbsp; &nbsp; for i, v := range dag{&nbsp; &nbsp; &nbsp; if v == someday {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i==6 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i = i + 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return dag[i]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return "no such day in a week"}
随时随地看视频慕课网APP

相关分类

Go
我要回答