c# - 我想从右到左的第二个对角矩阵在矩阵对角线上做一条交叉线
问题描述
我用维度数组和两个循环制作了 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();
}
}
我添加了一张照片希望你能看到它!
解决方案
我在这里有同样的问题。尝试使用 .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();
}
}
推荐阅读
- python - 拆分管道分隔的系列,按单独的系列分组,并在新列中返回每个拆分值的计数
- android - 如果 Recyclerview 正在显示怎么办,然后再次隐藏项目
- flask - 烧瓶实时消息的设计问题
- php - 如何在 laravel 中获取父类别下的所有产品
- node.js - 保护 express js 节点中的资产文件夹
- recursion - 计算 a^b 的逻辑,幂是关键字吗?
- python - Pandas 根据条件对列的子集进行排序
- python-3.x - 如何一次将所有用户输入在一行中推送到 Python 中的链接列表?
- python - np.sum 不返回精确的总和
- reactjs - 带有 Hooks 的地图函数中的 OnChange 更新所有字段