简体   繁体   English

c# - 使用 for 循环打印相关行的二维数组

[英]c# - 2d array printing relevant rows with a for loop

I'm writing a C# console app.我正在编写一个 C# 控制台应用程序。 The "app" is a poor mans matrix (1 and 0 fall down the page). “应用程序”是一个糟糕的曼矩阵(1 和 0 落在页面下方)。

I can't figure out how to get 1 row first, then two rows (the 1st row should now be in 2cnd place) etc.我不知道如何先得到 1 行,然后是两行(第一行现在应该在 2cnd 位置)等等。

The closest I get is just the next row in the 2d array...我得到的最接近的是二维数组中的下一行......

This prints just the current row:这仅打印当前行:

    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;
    }

I've modified it, and tried to get it to print out all the rows that have been printed so far +1, but I cant get it to work...我已经对其进行了修改,并试图让它打印出迄今为止打印的所有行+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;
    }

Any help would be much appreciated.任何帮助将非常感激。

Thank you谢谢

Edit 1:编辑1:

As requested I will show you the wished for result.根据要求,我将向您展示希望的结果。

Let's say my array has 3 rows.假设我的数组有 3 行。 The values are like this:值是这样的:

 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

I would like the 1st second/or user input to print我想打印第一秒/或用户输入

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

The 2nd user input to print:要打印的第二个用户输入:

(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

The 3rd user input to print:第三个用户输入打印:

(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

Thanks for pointing out what wasn't clear.感谢您指出不清楚的地方。 As I posed the question I though that was clear:)当我提出这个问题时,我虽然很清楚:)

Let me know if I can provide any additional info让我知道我是否可以提供任何其他信息

You've made things hard for yourself by using j, k, i, etc. - you can make the whole thing easier.通过使用 j、k、i 等,你让自己变得困难——你可以让整个事情变得更容易。 I'm assuming you're working off a 2 dimensional array of string.我假设您正在处理一个二维字符串数组。 Here's a simpler version (presuming I've understood your requirements correctly...)这是一个更简单的版本(假设我已经正确理解了您的要求......)

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
        }

Well, I figured it out.嗯,我想通了。 If it helps anyone...如果对任何人有帮助...

        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();
                }
            }
            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM