被难住了高手指点一下!我是初学者!急!!!!题目:编写一个类,能随机产生一个二维矩阵,要求:
//1、该类有二维数组、数组的行、高等成员;
//2、有构造函数;
//3、有add方法,实现矩阵的加法;
//4、有printarry方法,显示矩阵;
//5、在主程序中验证该类,实现两个矩阵的加法,并输出。
//6、上传定义类的源文件。
namespace work4
{
    class matrix
    {
        int[,] a;
        int x;
        int y;
        public matrix(int x, int y)
        {
            a = new int[x, y];
            Random m1 = m1.Next(100);
            this.x = x;
            this.y = y;
        }
        public matrix add(matrix a, matrix b)
        {
            matrix c = new matrix(4, 4);
            
            for(int i=0;i<x;i++)
                for(int j = 0;j<y;j++)
                    c[x, y] = a[x, y] + b[x, y];
            return c[x, y];        }

解决方案 »

  1.   


        class matrix
        {
            private int[,] a;
            private int x;
            private int y;
            public Int32 this[Int32 i, Int32 j]
            {
                get
                {
                    return a[i, j];
                }
                set
                {
                    a[i, j] = value;
                }
            }
            public Int32 X
            {
                get
                {
                    return x;
                }
            }
            public Int32 Y
            {
                get
                {
                    return y;
                }
            }
            public matrix(int x, int y)
            {
                this.x = x;
                this.y = y;
                a = new int[x, y];
                Random m1 = new Random();
                for (Int32 i = 0; i < x; i++)
                {
                    for (Int32 j = 0; j < y; j++)
                    {
                        a[i, j] = m1.Next(100);
                    }
                }        }
            public static matrix add(matrix a, matrix b)
            {
                if (!(a.X == b.X && a.Y == b.Y))
                    return null;            matrix m = new matrix(a.X,a.Y);            for (int i = 0; i < a.X; i++)
                    for (int j = 0; j < a.Y; j++)
                        m[i,j] = a[i,j] + b[i,j];            return m;
            }
            public void printarry()
            {
                for (Int32 i = 0; i < x; i++)
                {
                    for (Int32 j = 0; j < y; j++)
                    {
                        Console.Write(a[i,j] + "   ");
                    }
                    Console.WriteLine();
                }
            }    }
      

  2.   

    测试:        static void Main(string[] args)
            {
                matrix m1 = new matrix(2,3);
                matrix m2 = new matrix(2, 3);
                matrix m3 = matrix.add(m1,m2);
                m3.printarry();
             }