【发布时间】:2021-03-03 13:28:49
【问题描述】:
我收到multiplyMatrixByConstant(); 的错误消息,说“没有给出与所需形式参数相对应的参数”我不确定要包含哪些内容以允许我在 multiplyMatrixByConstant(); 方法中使用矩阵数组
public class Program
{
public static void Main()
{
Console.WriteLine(" ----------Welcome to the Matrix Program----------");
Console.WriteLine("Please select one of the following options:");
Console.WriteLine("1: The Random Matrix");
Console.WriteLine("2: The Transpose Matrix");
Console.WriteLine("3: Multiplying a Matrix by a Constant");
Console.WriteLine("4: Multiplying Two Matrices");
Console.Write("Your choice is: ");
string choice = Console.ReadLine();
if (choice == "1")
{
generateMatrix();
}
else if (choice == "2")
{
generateTranspose();
}
else if (choice == "3")
{
multiplyMatrixByConstant();
}
}
static int[, ] generateMatrix()
{
Console.Write("Enter the number of columns: ");
int c = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of rows: ");
int r = Convert.ToInt32(Console.ReadLine());
int[, ] matrix = new int[c, r];
Random rnd = new Random();
Console.WriteLine("The Matrix is: ");
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
matrix[i, x] = rnd.Next(0, 10);
}
}
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
Console.Write(matrix[i, x] + " ");
}
Console.WriteLine(" ");
}
return (matrix);
}
static void generateTranspose()
{
Console.Write("Enter the number of columns: ");
int c = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of rows: ");
int r = Convert.ToInt32(Console.ReadLine());
int[, ] matrix = new int[c, r];
Random rnd = new Random();
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
matrix[i, x] = rnd.Next(0, 10);
}
}
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
Console.Write(matrix[i, x] + " ");
}
Console.WriteLine(" ");
}
Console.WriteLine("The Transpose is:");
int[, ] transpose = new int[r, c];
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
transpose[x, i] = matrix[i, x];
}
}
for (int i = 0; i < r; i++)
{
for (int x = 0; x < c; x++)
{
Console.Write(transpose[i, x] + " ");
}
Console.WriteLine(" ");
}
}
static void multiplyMatrixByConstant(int[, ] matrix, int c, int r)
{
generateMatrix();
Console.Write(" Enter a Constant to Multiply the Matrix by: ");
int constant = Convert.ToInt32(Console.ReadLine());
int[, ] result = new int[c, r];
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
result[i, x] = matrix[i, x] * constant;
}
}
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
Console.Write(result[i, x] + " ");
}
Console.WriteLine(" ");
}
}
}
【问题讨论】:
-
请将您在完整堆栈跟踪中遇到的错误添加到问题中。
标签: c# arrays matrix multidimensional-array methods