首页 > 解决方案 > C# - 使用更新的数据在矩阵中打印 1 列

问题描述

我现在正在高中学习 C#。我是初学者,我正在尝试执行以下操作:

我有一个任务,我需要打印一个矩阵,使用案例更新它,然后用更新的数据打印其中一个列。首先,我创建了一个简单的矩阵,在 20 on 5 的正方形上打印 -1:

bankMatrix = new int[20, 5];
for (int i = 0; i < bankMatrix.GetLength(0); i++)
{
    for (int j = 0; j < bankMatrix.GetLength(1); j++)
    {
        Console.Write("{0,3}", bankMatrix[i, j] = -1);
    }
    Console.WriteLine();
}
Console.WriteLine();

输出以下内容:

 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1

接下来,用户通过 case 语句从 1-5 中选择一列,然后更新矩阵:

Console.WriteLine("please choose where would you like to create your account (we have 5 branches, choose one):");
                    int accountBranch = int.Parse(Console.ReadLine());
                    if (accountBranch > 5)
                    {
                        Console.WriteLine("we only have 5 branches. please choose from them");
                        break;
                    }
                    accountBranch = accountBranch - 1;
                    bool branch = false;
                    int i;
                    for (i = 0; i < bankMatrix.GetLength(0); i++)
                    {
                        if (bankMatrix[i, accountBranch] == -1)
                        {
                            branch = true;
                            break;
                        }
                        else
                        {
                            Console.WriteLine("sorry but this branch is full. please choose another 
                            branch.");
                            break;
                        }
                        if (branch)
                    {
                        bankMatrix[i, accountBranch] = 0;
                        Console.WriteLine("account saved on branch {0}, vault {1}", accountBranch + 1, i);
                    }

这是更新矩阵的代码:

for (int i = 0; i < bankMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < bankMatrix.GetLength(1); j++)
                {
                    Console.Write("{0,3}", bankMatrix[i, j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();

现在是我苦苦挣扎的部分。我需要使用更新的数据打印用户选择的列,例如,如果用户选择打印第 4 列,并且列顶部只有一个 0,它应该输出:

 0
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

我试图这样做:

Console.WriteLine("what branch?");
                        int branchShowBalance = int.Parse(Console.ReadLine());
                        branchShowBalance = branchShowBalance - 1;
                        int[,] balanceMatrix;
                        balanceMatrix = new int[20, 1];
                        for (int K = 0; K < balanceMatrix.GetLength(0); K++)
                        {
                            for (int H = 0; H < balanceMatrix.GetLength(1); H++)
                            {
                                Console.Write("{0,3}", balanceMatrix[K, H]);
                            }
                            Console.WriteLine();
                        }
                        Console.WriteLine();

但它只输出有很多 0 的列,而不是更新的数据:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

我应该怎么做才能解决这个问题?

谢谢!

标签: c#

解决方案


您再次非常接近解决方案。零的原因是您只是创建了新数组,但没有填充旧数组中的值。在 C# 中,整数的默认值为 0。因此,当您打印新矩阵时,您只会得到零。这是您的问题的解决方案:

        // Get column value
        Console.WriteLine("what branch?");
        int columnIndex = int.Parse(Console.ReadLine());
        columnIndex = columnIndex - 1;

        // Create new column matrix
        int[,] balanceMatrix = new int[20, 1];

        // Set new matrix values from the bankMatrix
        for (int rowIndex = 0; rowIndex < balanceMatrix.GetLength(0); rowIndex++)
        {
            balanceMatrix[rowIndex, 0] = bankMatrix[rowIndex, columnIndex];
        }

        // Print values
        for (int K = 0; K < balanceMatrix.GetLength(0); K++)
        {
            for (int H = 0; H < balanceMatrix.GetLength(1); H++)
            {
                Console.Write("{0,3}", balanceMatrix[K, H]);
            }
            Console.WriteLine();
        }

推荐阅读