我想在矩阵对角线中从右到左从右到左的第二个对角矩阵中划一条交叉线



我用维度数组和两个循环制作了矩阵对角线 我有两个crosa一个通过从右到左更改颜色线按钮如何做到这一点?

在此处输入图像描述

这是我的代码,我从左到右按钮划了交叉线 现在我想从右到左按钮 这是我的代码

static void DisplayMatrixWithCross(int[,] matrix)
{
for (int row = 0; row < 7; row++)
{
for (int column = 0; column < 7; column++)
{
if (row == column)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(matrix[row, column] + " ");
}
else if ()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(matrix[row, column] + " ");
}
else {
Console.ForegroundColor = ConsoleColor.White;
Console.Write(matrix[row, column] + " ");
}
}
Console.WriteLine();
}
}

我添加了一张照片,希望你能看到它!

我在这里遇到了同样的问题。尝试使用这些方法。GetLength(0( 和 .获取长度(1(. 您只需要两个 IF 语句。 让我向您展示我的代码,到目前为止,这就是我所拥有的一切:

static void DisplayMatrixWithCross(int[,] matrix)
{
for (int row = 0; row < matrix.GetLength(0); row++)
{

for (int col = 0; col < matrix.GetLength(1); col++)
{
if (row == (col - 5) + 5)
{
Console.BackgroundColor = ConsoleColor.Green;
}
Console.ResetColor();
if (row == (col + 5) - 5) 
{
Console.ForegroundColor = ConsoleColor.Red;
}

Console.Write("{0,3} ", matrix[row, col].ToString("00"));
}
Console.WriteLine();
}
}

最新更新