【问题标题】:I wan to make a cross line in Matrix diagonal from 2nd diagonal matrix from right to left我想从右到左的第二个对角矩阵在矩阵对角线上做一条交叉线
【发布时间】:2020-03-23 00:31:33
【问题描述】:

我用维数组和两个循环制作了矩阵对角线 我有两个 crosa 一个,通过将颜色线从右到左按钮更改我该怎么做?

enter image description here

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

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#


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      相关资源
      最近更新 更多