java - 在 if 语句中迭代数组
问题描述
我正在用可定制的规则构建生活游戏。我从一个函数接收一个数字数组,我需要遍历该数组以获取一组数字。我需要在不知道数组长度的情况下将这些数字添加到 if 语句中,以更新生活游戏的规则。以下是代码示例:
if(nAlive < 2)
next[i][j] = false;
else if(nAlive == 2 || nAlive == 3)
next[i][j] = true;
else
next[i][j] = false;
我需要的是(这是伪代码):
if(nAlive == surviving[0] || nAlive == surviving[1] || ... nAlive == surviving[n])
next[i][j] = true;
else
next[i][j] = false;
解决方案
答案是:
if(alive[i][j])
{
next[i][j] = false;
for (int x = 0; x < surviving.length; x++) {
if (surviving[x] == nAlive)
next[i][j] = true;
}
}
推荐阅读
- docker - 在本地机器上运行 `docker-compose up` 时出现 FileNotFound 错误
- python - 如果给定的字符串与字典中的键匹配,如何返回键的值
- ruby-on-rails - Rails Action cable:如何将房间 ID 从客户端 channel.js 安全地传递到服务器 channel.rb
- java - 在 Spring 中允许带有重定向的帧显示
- ios - 如何使用客户的开发者帐户将应用程序发布到 App Store?
- xcode - LaunchScreen.storyboard 未显示
- python - 朴素高斯预测概率仅返回 0 或 1
- python - Python:标记关键字并使用 1 和 0 创建新的标签列
- python - 如何使用来自 stripe webhook ex) 订阅、django 中的客户的数据更新当前登录用户的字段?
- sql - SQL左连接仅在我指定右表的列时才有效(Redshift)