0

我在学校的这个项目上遇到了一些问题。我正在尝试使用二维数组,并收到一些关于“没有从 int 转换为 int * 和 '>=':'int [5]' 与 'int' 的间接级别不同”的错误。我可以为一维数组编写它,但对二维的语法有困难。关于我可能缺少的东西,有人可以指出我正确的方向吗?我在 btnShow_CLick 之后注释掉了它并且它工作正常,它只是 btnGroup_Click 我显然遗漏了一些东西。

感谢任何可能分享一些知识的人。

    static const int NUMROWS = 4;
    static const int NUMCOLS = 5;
    int row, col;
    Graphics^ g;
    Brush^ redBrush;
    Brush^ yellowBrush;
    Brush^ greenBrush;
    Pen^ blackPen;


private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             g = panel1->CreateGraphics();
             redBrush = gcnew SolidBrush(Color::Red);
             yellowBrush = gcnew SolidBrush(Color::Yellow);
             greenBrush = gcnew SolidBrush(Color::Green);
             blackPen = gcnew Pen(Color::Black);
         }

    private: System::Void btnShow_Click(System::Object^  sender, System::EventArgs^  e) {

         panel1->Refresh();

         for (int row = 0; row < NUMROWS; row++)
         {
             for (int col = 0; col < NUMCOLS; col++)
             {
                 Rectangle seat = Rectangle(75 + col * 75,40 + row *40,25,25);
                 g->DrawRectangle(blackPen, seat);
             }
         }
     }

private: System::Void btnGroup_Click(System::Object^  sender, System::EventArgs^  e) {
             int score[NUMROWS][NUMCOLS] = {{45,65,11,98,66},
                                        {56,77,78,56,56},
                                        {87,71,78,90,78},
                                        {76,75,72,79,83}};

         int mean;
         int student;
         mean = CalcMean(score[]);
         txtMean->Text = mean.ToString();

         for (int row = 0; row < NUMROWS; row++)
         {
             for (int col = 0; col < NUMCOLS; col++)
             {
                 student = (row*NUMCOLS) + (col);
                 Rectangle seat = Rectangle(75 + col * 75,40 + (row * 40),25,25);
                 if (score[student] >= 80
                     g->FillRectangle(greenBrush, seat);
                 else if (score[student] >= mean)
                     g->FillRectangle(yellowBrush, seat);
                 else 
                     g->FillRectangle(yellowBrush, seat);
                 g->DrawRectangle(blackPen, seat);
             }
         }
     }

     private: double CalcMean(int score[])
     {
         int sum = 0;
         int students = NUMROWS * NUMCOLS;
         for (int i=0; i< students; i++) sum += score[i];
         return sum / students;
     }
4

1 回答 1

1

Score[student]等价于*(score+student),这是一个*int。相反,您可能应该使用score[row][col],或其等价物**(score+student)(我强烈建议使用数组表示法)。它也等同于*Score[student],但这很丑陋。

另外,当我说“它是等价的”时,只是因为sizeof int ==sizeof (*int). 如果您将指针逻辑与数组中的另一种类型一起使用,您可能会得到奇怪的结果。

于 2012-08-05T01:20:07.230 回答