为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
炎炎设计 2022-07-11 16:23:26
我正在尝试创建一个不和谐的机器人来检查消息是否包含存储在数组中的字符串。我尝试使用 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    }
查看完整描述

1 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

只需使用一个循环:


for i := 0; i <= 6; i ++ {

    if strings.Contains(mContent, characeterss[i]) {

        currentCharacter = characterss[i]

        currentDescription = descriptions[i]

        currentImage = images[i]

        isCorrect = true

        break

    }

}


查看完整回答
反对 回复 2022-07-11
  • 1 回答
  • 0 关注
  • 77 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信