C# 打印从函数返回的2D数组
我有一个从函数(c#)返回的2d数组,我想打印它(以显示新的矩阵) 代码如下:C# 打印从函数返回的2D数组,c#,function,multidimensional-array,C#,Function,Multidimensional Array,我有一个从函数(c#)返回的2d数组,我想打印它(以显示新的矩阵) 代码如下: using System; using System.Dynamic; namespace Quera { class Matrix { private int r1; private int c1; private int[,] mat1; public int[,] multiply_matrix(int r2, int c2)
using System;
using System.Dynamic;
namespace Quera
{
class Matrix
{
private int r1;
private int c1;
private int[,] mat1;
public int[,] multiply_matrix(int r2, int c2)
{
int[,] mat2 = new int[r2, c2];
int[,] mat3 = new int[r1, c2];
// Enter data mat2
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
mat2[i, j] = Console.Read();
}
}
// Multiply
if (c1 != r2)
{
Console.WriteLine("Matrix multiplication not possible");
}
else
{
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
mat3[i, j] = 0;
for (int k = 0; k < c1; k++)
{
mat3[i, j] += mat1[i, k] * mat2[k, j];
}
}
}
}
return mat3;
}
public Matrix(int r, int c)
{
mat1 = new int[r, c];
r1 = r;
c1 = c;
// Enter data
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
mat1[i, j] = Console.Read();
}
}
}
}
class Program
{
static void Main(string[] args)
{
string[] tokens = Console.ReadLine().Split();
int[] enteries = new int[2];
enteries[0] = Convert.ToInt32(tokens[0]);
enteries[1] = Convert.ToInt32(tokens[1]);
string[] token = Console.ReadLine().Split();
int[] entery = new int[2];
entery[0] = Convert.ToInt32(token[0]); // you have to pass it to multiply_matrix
entery[1] = Convert.ToInt32(token[1]); // you have to pass it to multiply_matrix
Matrix matrix = new Matrix(enteries[0], enteries[1]);
Console.WriteLine(**???**);
}
}
}
使用系统;
运用系统动力学;
命名空间查询
{
类矩阵
{
私有int r1;
私人int c1;
私有int[,]mat1;
公共整数[,]乘法矩阵(整数r2,整数c2)
{
int[,]mat2=新的int[r2,c2];
int[,]mat3=新int[r1,c2];
//输入数据mat2
对于(int i=0;i
我应该在“?””部分(最后一行代码)中写什么来打印整个数组(从乘法矩阵函数返回的mat3)?
我试着编写Console.WriteLine(matrix.multiply_matrix(entry[0],entry[1])[0]);虽然我没有得到任何答案,但有人告诉我,我可以创建一个对象(矩阵),而不是返回一个
打印二维阵列是一个在许多地方反复解决的问题。请尝试搜索解决方案,尝试自己编写代码,如果遇到问题,请告诉我们问题所在。这是否回答了您的问题@你有我的问题吗wrong@tnw我知道如何打印2d数组,问题是如何从函数返回的数组中获取元素(主要问题是我无法从函数中获取任何内容)。公平地说,您的问题的提出表明您需要帮助打印数组,不是如何在
Main
中实际访问mat3
。