微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决数组循环引发读取访问异常

我正在使用干净的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` 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。