首页 > 解决方案 > 我想从右到左的第二个对角矩阵在矩阵对角线上做一条交叉线

问题描述

我用维度数组和两个循环制作了 Matrix 对角线,我通过从右到左按钮更改颜色线有两个 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();

            }

        }

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

标签: c#

解决方案


我在这里有同样的问题。尝试使用 .GetLength(0) 和 .GetLength(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();
    }
}

推荐阅读