if(robots[0] != null && !robots[0].isStrong()) {
if(robots[1] != null &&!robots[1].isStrong()) {
if(robots[2] != null &&!robots[2].isStrong()) {
//do something
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想检查一下所有的机器人是否都不坚固,如果它们都不坚固,那么就做点什么吧.想象一下,我有10个机器人,如果有条件我是否必须写10个?
假设您要检查这些条件是否适用于robots数组的每个元素,以及您使用的是Java 8或更高版本:
if (Arrays.stream(robots).allMatch(r -> r!=null && !r.isStrong())) {
// do whatever
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
133 次 |
| 最近记录: |