gpt4 book ai didi

kotlin - 在 Kotlin 中将WHEN和LIST与CONTAINS结合

转载 作者:行者123 更新时间:2023-12-02 13:28:45 24 4
gpt4 key购买 nike

如果我确实想在女友或我最好的 friend 在那里时参加聚会,无论如何。如果他们不在那里,如果有一个凶手,我就不会去。但是,如果聚会上没有凶手,那么如果有有趣的皮特在场,我就走了。
以下是无效的Kotlin代码,因为我不能以这种方式使用.contains。有什么类似的简洁方法可以编写此代码?

val peopleAtParty = listOf("girlfriend","bestFriend","murderer","funnyPete")

val goingToParty = when(peopleAtParty.contains) {
"girlfriend","bestFriend" -> true
"murderer" -> false
"funnyPete" -> true
else -> false
}

最佳答案

这样尝试

    val goingToParty = when {
peopleAtParty.contains("girlfriend") -> true
peopleAtParty.contains("bestFriend") -> true
peopleAtParty.contains("murderer") -> false
peopleAtParty.contains("funnyPete") -> true
else -> false
}

关于kotlin - 在 Kotlin 中将WHEN和LIST与CONTAINS结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62584663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com