c#-2d数组使用for循环打印相关行

时间:2020-08-13 15:21:07

标签: c# arrays for-loop

我正在编写C#控制台应用程序。 “ app”是一个穷人矩阵(1和0落在页面上)。

我不知道如何先获得1行,然后获得2行(第一行现在应位于2cnd位置),等等。

我得到的最接近的只是2d数组中的下一行...

这仅显示当前行:

    public static int col = 0;

    static void PrintSingleLine()
    {
        for (int i = col; i <= pnms.GetLength(0) - 1;)
        {
            for (int j = 0; j <= pnms.GetLength(1) - 1; j++)
            {
                Console.Write(pnms[col, j]);
            }
            break;
        }
        col =+ 1;
    }

我已经对其进行了修改,并试图使它打印出到目前为止已打印的所有行+1,但我无法使其正常工作...

    public static int coll = 0;

    static void PrintRelevantLines()
    {
        int cnt = 0;

        for (int i = coll; i <= pnms.GetLength(0) - 1;)
        {
            for (int j = 0; j <= pnms.GetLength(1) - 1; j++)
            {
                for (int k = cnt; k <= coll; k++)
                {
                    Console.Write(pnms[k, j]);
                }
            }
            break;
        }
        coll = +1;
    }

任何帮助将不胜感激。

谢谢

编辑1:

根据要求,我将向您显示期望的结果。

假设我的数组有3行。值是这样的:

 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1
 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1
 1 0 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1

我想打印第一秒/或用户输入

(x) 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1

要打印的第二个用户输入:

(y) 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1
(x) 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1

要打印的第三个用户输入:

(z) 1 0 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1
(y) 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1
(x) 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1

感谢您指出不清楚的地方。当我提出问题时,我虽然很清楚:)

让我知道是否可以提供其他信息

2 个答案:

答案 0 :(得分:1)

通过使用j,k,i等,您已经使自己变得困难起来-您可以使整个过程变得容易。我假设您正在处理字符串的二维数组。这是一个简单的版本(假设我已经正确理解了您的要求...)

static void PrintRelevantLines()
        {
            string [,]  pnms = new string[3,5];
            // Setup data
            pnms[0, 0] = "1";
            pnms[0, 1] = "0";
            pnms[0, 2] = "1";
            pnms[0, 3] = "0";
            pnms[0, 4] = "1";

            pnms[1, 0] = "0";
            pnms[1, 1] = "1";
            pnms[1, 2] = "0";
            pnms[1, 3] = "1";
            pnms[1, 4] = "0";

            pnms[2, 0] = "1";
            pnms[2, 1] = "1";
            pnms[2, 2] = "1";
            pnms[2, 3] = "0";
            pnms[2, 4] = "0";

            for (int columnIndex = 0; columnIndex <= pnms.GetLength(0) - 1; columnIndex ++)
            {
                for (int rowIndex = 0; rowIndex <= pnms.GetLength(1) - 1; rowIndex++)
                {
                        Console.Write(pnms[columnIndex, rowIndex]);
                }

                Console.WriteLine(); // (if you wanted a matrix style trickle of lines, you'd probably want some sort of pause here....)                  
            }

                // OUTPUT
                // 10101
                // 01010
                // 11100
        }

答案 1 :(得分:0)

好吧,我知道了。如果可以帮助任何人...

        public static int coll = 0;
        static void PrintRelevantLines()
        {
            Console.Clear();
            if (coll <= pnms.GetLength(0) - 1)
            {
                for (int k = 0; k <= coll; k++)
                {
                    for (int j = 0; j <= pnms.GetLength(1) - 1; j++)
                    {
                        Console.Write(pnms[k, j]);
                    }
                    Console.WriteLine();
                }
            }