c - continue 语句的替代方案
问题描述
我正在寻找一种方法来替换此函数中的 continue 语句。houserules 声明它们不能使用,但我很难实现不会导致其余代码无法正常运行的替换。
bool neighCheck (int i, int j, int a[][COLLENGTH])
{
bool neighbourOnFire;
int x, y, neighX, neighY, curreNeigh;
/* Bool set up to change after neighbours looped*/
neighbourOnFire = false;
/* The neighbours -looping from -1 -> 1 to get index of each neighbour*/
for (x = -1; x < 2; x++) {
for (y = -1; y < 2; y++) {
/* Disregards current (middle) cell*/
if ((x == 0) && (y == 0)) {
continue;
}
/* Get indexes of the neighbour we're looking at */
neighX = i + x;
neighY = j + y;
/* Checks for edges*/
if (neighX >= 0 && neighY >= 0 && neighX < ROWLENGTH
&& neighY < COLLENGTH) {
/* Get the neighbour using the indexes above */
curreNeigh = a[neighX][neighY];
/* Test to see if the neighbour is burning */
if (curreNeigh == fire) {
neighbourOnFire = true;
continue;
}
}
}
}
return neighbourOnFire;
}
解决方案
第一个continue;
可以通过反转条件并将其余代码放在if
语句中来替换。
第二个continue;
可以简单地删除,因为之后没有要执行的代码。
bool neighCheck (int i, int j, int a[][COLLENGTH])
{
bool neighbourOnFire;
int x, y, neighX, neighY, curreNeigh;
/* Bool set up to change after neighbours looped*/
neighbourOnFire = false;
/* The neighbours -looping from -1 -> 1 to get index of each neighbour*/
for (x = -1; x < 2; x++) {
for (y = -1; y < 2; y++) {
/* Disregards current (middle) cell*/
if (!((x == 0) && (y == 0))) {
/* Get indexes of the neighbour we're looking at */
neighX = i + x;
neighY = j + y;
/* Checks for edges*/
if (neighX >= 0 && neighY >= 0 && neighX < ROWLENGTH
&& neighY < COLLENGTH) {
/* Get the neighbour using the indexes above */
curreNeigh = a[neighX][neighY];
/* Test to see if the neighbour is burning */
if (curreNeigh == fire) {
neighbourOnFire = true;
}
}
}
}
}
return neighbourOnFire;
}
推荐阅读
- kubernetes - 当 Pod 缩放到零时,Knative 如何将流量切换到激活器
- java - 使用 Gluon 客户端插件构建 APK - org/apache/log4j/Priority ClassNotFoundException
- typescript - 如何定义一种与其他属性不同的属性类型?
- node.js - live-server 不会在 Mac BigSur 上全局安装
- dolphindb - 关于在 dolphindb 中从磁盘加载持久性数据
- android - 如何重组凌乱的android导航图?
- angular - 获取方法调用不附加承载授权,而发布请求工作正常
- python - Maya Python:在曲线上找到中点变换
- node.js - 反应expo cli npm错误的本机安装
- node.js - 如何在猫鼬中引用找到的文档的字段?