对于循环逻辑错误?

blu*_*ckk 1 c++

void GameBoard::print(const GameBoard& computerBoard)
{
Grid[0][0] = '1';
Grid[0][1] = '2';
Grid[1][0] = '3';

int i, j;
int sides = SIZE;

cout << "        Your bombs:                    Your navy:" << endl << endl;

for(i=0; i<SIZE; i++)
{
    // prints your bombs
    cout << sides << "  ";
    for(j=0; j<SIZE; j++)
    {
        cout << computerBoard.Grid[i][j] << "  ";
    }
    cout << "    ";

    // prints your ships
    cout << sides << "  ";
    for(j=0; j<SIZE; j++)
    {
        cout << Grid[i][j] << "  ";
    }
    cout << endl << endl;
    sides--;
}

j = 0;
cout << "\n   ";

for(i=0; i<SIZE; i++)
{
    j = i + 'A';
    cout << (char)j << "  ";
}
cout << "       ";

for(i=0; i<SIZE; i++)
{
    j = i + 'A';
    cout << (char)j << "  ";
}
cout << endl << endl;
Run Code Online (Sandbox Code Playgroud)

}

我正在建造一个像战舰这样的游戏,需要更改for循环来阅读..

for(i=8;i>0;i--)
Run Code Online (Sandbox Code Playgroud)

为什么会产生错误?

很抱歉,如果这没有意义,Grid [0] [0]应位于左下角,但它当前位于左上角.

sch*_*der 8

我猜你想要的是什么样的

for(i=SIZE-1; i>=0; i--)
Run Code Online (Sandbox Code Playgroud)

因为这相当于

for(i=0; i<SIZE; i++)
Run Code Online (Sandbox Code Playgroud)

这两个从去0..SIZE-1

for(i=SIZE; i>0; i--)
Run Code Online (Sandbox Code Playgroud)

从去1..SIZE(和因此,如果访问长度的阵列SIZE使用arr[i],将产生错误).