[英]Can not add values to a multidimensional array in C#
I am new to C#. 我是C#的新手。 I am trying to make a matrix calculator but first I need the user to give me the size of and the contents of said matrix. 我正在尝试制作矩阵计算器,但是首先我需要用户给我所述矩阵的大小和内容。 I have 2 classes. 我有2节课。
First class is as follows: 头等舱如下:
class Assignment1
{
static void Main(string[] args)
{
Console.Write("Please enter the number of rows in the matrix: ");
int row = int.Parse(Console.ReadLine());
Console.Write("Please enter the number of columns in the matrix: ");
int columns = int.Parse(Console.ReadLine());
MatrixN matrix =new MatrixN(row, columns);
int i = 0;
for (double x = 0; x < row; x++)
{
for(double y = 0; y < columns; y++)
{
if(i == 0)
{
Console.Write("Enter first value of the matrix: ");
matrix[x, y] = double.Parse(Console.ReadLine());
i++;
}
else if (i == row * columns)
{
Console.Write("Enter last value of the matrix: ");
matrix[x, y] = double.Parse(Console.ReadLine());
i++;
}
Console.Write("Enter nest value of the matrix: ");
matrix[x, y] = double.Parse(Console.ReadLine());
i++;
}
}
}
}
Second class is: 第二类是:
class MatrixN
{
double[,] m;
public MatrixN(int row, int column)
{
m = new double[row, column];
}
I keep getting the error: cannot apply indexing with [] to an expression of MatrixN for the code 我不断收到错误: 无法将带有[]的索引应用于代码的MatrixN表达式
matrix[x, y] = double.Parse(Console.ReadLine());
Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。
matrix
is a variable of type MatrixN
. matrix
是MatrixN
类型的变量。 The type MatrixN
does not support the subscription/indexing operation, so you cannot access the value matrix[i, j]
, nor set the value matrix[i, j] = v;
MatrixN
类型不支持预订/索引操作,因此您无法访问值matrix[i, j]
,也无法设置值matrix[i, j] = v;
. 。
You can either expose the m
member variable of the MatrixN
class (by making it public), and set/access the value of matrix.m
, or write your own index operator: 您可以公开MatrixN
类的m
成员变量(将其公开),并设置/访问matrix.m
的值,或者编写自己的索引运算符:
class MatrixN {
private double[,] inner;
public double this[int x, int y] {
get { return inner[x, y]; }
set { inner[x, y] = value; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.