数组循环引发读取访问异常

问题描述

我正在使用干净的C ++编写一个简单的战舰游戏机游戏。我正在尝试编写一个方法,它将返回船的许多甲板。看起来像这样:

int Field::get_deck_number(int x,int y)
{
int temp_x = x,temp_y = y,deck_counter = 0;
if (arr[y][x] == field_sign || arr[y][x] == border_sign)
{
    return 0;
}
for (int direction = 0; direction < 4; direction++)
{
    temp_x = x;
    temp_y = y;
    while (arr[temp_y][temp_x] != field_sign || arr[temp_y][temp_x] != border_sign || arr[temp_y][temp_x] != tried_sign)
    {
        if (arr[temp_y][temp_x] == ship_sign || arr[temp_y][temp_x] == destroyed_sign)deck_counter++;
        if (direction == 0)
        {
            temp_y++;
            continue;
        }
        if (direction == 1)
        {
            temp_y--;
            continue;
        }
        if (direction == 2)
        {
            temp_x++;
            continue;
        }
        if (direction == 3)
        {
            temp_x--;
            continue;
        }
    }
}
return deck_counter;
}

问题在于while循环条件。这是一个无限循环,因此经过几次迭代后,我将遇到读取访问冲突。

解决方法

问题在于逻辑OR和AND(||,&&)的使用不当。我还在代码中发现了其他逻辑问题。现在它可以正常工作了。

SELECT 
  SUM(`turnovers`) - SUM(`costs`) as `profitA`,SUM(`turnovers` - `costs`) as `profitB` 
FROM `mytable` 

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...