二到无限大_ 二到无限大_
关注数: 1 粉丝数: 20 发帖数: 214 关注贴吧数: 7
索引超出了数组界限 郁闷 咋回事 新手求指教 using System; class Matrix { int[,] matrix; public int row,col; public Matrix() { } public Matrix(int row,int col) { this.row=row; this.col=col; matrix=new int[row,col]; } public Matrix(Matrix m) { int row=m.row; int col=m.col; matrix=new int[row,col]; for(int i=0;i<row;i++) for(int j=0;j<col;j++) matrix[i,j]=m.getNum(i,j); } public void SetNum(int i,int j,int num) { matrix[i,j]=num; } public int getNum(int i,int j) { return matrix[i,j]; } public void print() { for(int p=0;p<row;p++) { for(int q=0;q<col;q++) { Console.Write("{0}\t",matrix[p,q]); } Console.Write("\n"); } } public void InputM(int Row,int Col) { for(int a=0;a<Row;a++) { for(int b=0;b<Col;b++) { int value=Convert.ToInt32(Console.ReadLine()); this.SetNum(a,b,value); } } } public int[,] Detail { get{return matrix;} set{matrix=value;} } public Matrix transpose() { Matrix another=new Matrix(row,col); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { another.SetNum(j,i,matrix[i,j]); } } return another;; } public static Matrix add(Matrix lm,Matrix rm) { if(lm.row !=rm.row) { System.Exception e=new Exception("相加的两个矩阵的行数不等"); throw e; } if(lm.col !=rm.col) { System.Exception e=new Exception("相加的两个矩阵的列数不等"); throw e; } Matrix another=new Matrix(lm.row,lm.col); for(int i=0;i<lm.row;i++) { for(int j=0;j<lm.col;j++) { int temp=lm.getNum(i,j)+rm.getNum(i,j); another.SetNum(i,j,temp); } } return another; } public static void Main() { int Row,Col; Console.WriteLine("请输入想要的矩阵行数与列数"); Row=Convert.ToInt32(Console.ReadLine()); Col=Convert.ToInt32(Console.ReadLine()); Matrix m=new Matrix(Row,Col); Console.WriteLine("请输入矩阵数据"); m.InputM(Row,Col); m.print(); Matrix n=m.transpose(); n.print(); Console.WriteLine("请输入第二个矩阵行数与列数"); Row=Convert.ToInt32(Console.ReadLine()); Col=Convert.ToInt32(Console.ReadLine()); Matrix m2=new Matrix(Row,Col); Console.WriteLine("输入矩阵数据"); m2.InputM(Row,Col); Console.WriteLine("第二个矩阵为:"); m2.print(); Matrix result=add(m,m2); result.print(); } }这究竟是哪错了!!!!
1 下一页