如何用几行代码改进这个程序?

我正在尝试创建一个不和谐的机器人来检查消息是否包含存储在数组中的字符串。我尝试使用 for 循环,但它没有工作,因为它永远持续下去,我想减少代码行,有什么办法吗?


switch {

    case strings.Contains(mContent, characterss[0]):

        currentCharacter = characterss[0]

        currentDescription = descriptions[0]

        currentImage = images[0]

        isCorrect = true

    case strings.Contains(mContent, characterss[1]):

        currentCharacter = characterss[1]

        currentDescription = descriptions[1]

        currentImage = images[1]

        isCorrect = true

    case strings.Contains(mContent, characterss[2]):

        currentCharacter = characterss[2]

        currentDescription = descriptions[2]

        currentImage = images[2]

        isCorrect = true

    case strings.Contains(mContent, characterss[3]):

        currentCharacter = characterss[3]

        currentDescription = descriptions[3]

        currentImage = images[3]

        isCorrect = true

    case strings.Contains(mContent, characterss[4]):

        currentCharacter = characterss[4]

        currentDescription = descriptions[4]

        currentImage = images[4]

        isCorrect = true

    case strings.Contains(mContent, characterss[5]):

        currentCharacter = characterss[5]

        currentDescription = descriptions[5]

        currentImage = images[5]

        isCorrect = true

    case strings.Contains(mContent, characterss[6]):

        currentCharacter = characterss[6]

        currentDescription = descriptions[6]

        currentImage = images[6]

        isCorrect = true

    }


炎炎设计
浏览 78回答 1
1回答

HUH函数

只需使用一个循环:for i := 0; i <= 6; i ++ {&nbsp; &nbsp; if strings.Contains(mContent, characeterss[i]) {&nbsp; &nbsp; &nbsp; &nbsp; currentCharacter = characterss[i]&nbsp; &nbsp; &nbsp; &nbsp; currentDescription = descriptions[i]&nbsp; &nbsp; &nbsp; &nbsp; currentImage = images[i]&nbsp; &nbsp; &nbsp; &nbsp; isCorrect = true&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go