kotlin - 在 kotlin 中如何简化?
问题描述
我正在开发一个使用 when 语句的应用程序
我怎样才能使它更短?
when(page) {
0 -> poster[0].imageURL
1 -> poster[1].imageURL
2 -> poster[2].imageURL
3 -> poster[3].imageURL
else -> "image not provided"
}
解决方案
您可以将其替换为范围检查:
if (page in 0..3) {
poster[page].imageURL
} else {
"image not provided"
}
推荐阅读
- c# - 我如何知道我的串行通讯是否被 GC 关闭?
- amazon-web-services - 具有持久会话的 AWS IOT MQTT - 订阅失败
- .net-core - Entity Framework Core:如何返回可查询的
与“使用”? - html - 为不同的项目获得多个不同的评级以工作
- python - 如何复制 matplotlib 标准化功能 imshow
- java - 来自 Set.of(...) 的随机迭代顺序
- python - 迭代df列列表并用字典中的值替换现有键有效python
- c++ - 理解operator()(函数对象)的使用
- python - 计算列表中的第一个数字,如二十一点纸牌游戏
- php - 如何仅通过一个查询从 mysql 数据库中获取多个数据?