我试图更改2D数组中的元素,但它不会发生变化

时间:2018-02-23 08:26:53

标签: visual-c++

我尝试使用用户输入更改2D数组中的元素但不会更改。我该如何解决这个问题?我在网上寻找解决方案,但我找到的解决方案并没有解决我的问题。 编辑:我让它部分工作,但现在它改变了2D数组中的其他元素,如果您输入例如2A。

void grid()
        {
            int gridLength = 8;
            int gridWidth = 8;
            int index = 0;
            char gridLetter;
            int gridNumber;
            int numGrid[7][7];

            for (int pi = 0; pi <= 7; pi++)
            {
                for (int oi = 0; oi <= 7; oi++)
                {
                    numGrid[pi][oi] = 0;
                }
            }
            int ooo = 3;
            do
            {
                system("cls");
                int m = 0;
                char letter[8] = { 'A','B','C','D','E','F','G','H' };
                cout << "    1    2    3    4    5    6    7    8  " << endl;
                cout << endl;
                for (int l = 0; l < 1; l++)
                {
                            for (int Jindex = 0; Jindex < 8; Jindex++)
                            {
                                cout << letter[m] << "   ";
                                m++;
                                for (int index = 0; index < 8; index++)
                                {
                                    cout << numGrid[Jindex][index] << "    ";
                                }
                                cout << endl;
                                cout << endl;
                            }
                }
                cout << "Chose which tile you want to attack(press enter after each selection (ie. A *enter* 4) " << endl;
                cin >> gridLetter >> gridNumber;
                int gridLetters;
                switch (gridLetter)
                {
                case 'A':
                    gridLetters = 0;
                    gridNumber -= 1;
                    numGrid[gridLetters][gridNumber] = 1;
                case 'B':
                    gridLetters = 1;
                    gridNumber -= 1;
                    numGrid[gridLetters][gridNumber] = 1;
                case 'C':
                    gridLetters = 2;
                    gridNumber -= 1;
                    numGrid[gridLetters][gridNumber] = 1;
                case 'D':
                    gridLetters = 3;
                    gridNumber -= 1;
                    numGrid[gridLetters][gridNumber] = 1;
                case 'E':
                    gridLetters = 4;
                    gridNumber -= 1;
                    numGrid[gridLetters][gridNumber] = 1;
                case 'F':
                    gridLetters = 5;
                    gridNumber -= 1;
                    numGrid[gridLetters][gridNumber] = 1;
                case 'G':
                    gridLetters = 6;
                    gridNumber -= 1;
                    numGrid[gridLetters][gridNumber] = 1;
                case 'H':
                    gridLetters = 7;
                    gridNumber -= 1;
                    numGrid[gridLetters][gridNumber] = 1;
                }


            } while (ooo = 2);
        }

2 个答案:

答案 0 :(得分:0)

删除 ==在这些行中使用= numGrid[gridLetters][gridNumber] == 1;

答案 1 :(得分:0)

我尝试了你的代码&amp;我使用这个数组测试了id,

int numGrid[2][4]{
    { 1, 2, 3, 4 },
    { 5, 6, 7, 8 }
};

....在你的这个转换中你有几个声明测试1&amp;一些被赋值为1 ....我想这是一个类型错误,你打算将它们全部分配。 你的for循环返回垃圾数。 假设你的意思......

for (int m = 0; m < _countof(letter); m++)
        {
            cout << letter[m] << "   ";
            for (int i{}; i < _countof(numGrid); i++)
            {

                for (int j{}; j < _countof(*numGrid); j++)
                {
                    cout << numGrid[i][j];
                }

            }
                cout << endl;
        }

但我可能错了......

我看到一个屏幕,从个人的'A'到'H'显示 行后跟数字1-8。 我选择了一些像A4这样的值...它似乎通过你的循环增加了gridNumber 4 ...

switch (gridLetter)
    {
    case 'A':
        gridLetters = 0;
        //gridNumber -= 1;
        numGrid[gridLetters][gridNumber - 1] = 1;

    case 'B':
        gridLetters = 1;
        //gridNumber -= 1;
        numGrid[gridLetters][gridNumber - 5] = 1;

    case 'C':
        gridLetters = 2;
        //gridNumber -= 1;
        numGrid[gridLetters][gridNumber - 9] = 1;

    case 'D':
        gridLetters = 3;
        //gridNumber -= 1;
        numGrid[gridLetters][gridNumber - 13] = 1;

    case 'E':
        gridLetters = 4;
        //gridNumber -= 1;
        numGrid[gridLetters][gridNumber - 17] = 1;

    case 'F':
        gridLetters = 5;
        //gridNumber -= 1;
        numGrid[gridLetters][gridNumber - 21] = 1;

    case 'G':
        gridLetters = 6;
        //gridNumber -= 1;
        numGrid[gridLetters][gridNumber - 25] = 1;

    case 'H':
        gridLetters = 7;
        //gridNumber -= 1;
        numGrid[gridLetters][gridNumber - 29] = 1;

    }

我不知道你想怎么出来......但是我看起来不对,我会用一个函数, 你能发布整个节目吗?