首页 > 解决方案 > 创建修改二维数组元素的算法时遇到问题

问题描述

我在编辑二维字符数组的值时遇到问题。

char[,] chrRaster = new char[4, 5];

将值添加到数组并将其打印到控制台后,我得到:

// Input example:
*****
**.**
*****
****.

我正在尝试制作一种算法来替换“。”旁边、下方或上方的每个“*”。由“。” 然后将其打印到控制台。

// Output after algorithm example:
**.**
*...*
**.*.
***..

我尝试将 2d char 数组转换为 2d 字符串数组,然后IndexOf('*')用于替换'*'a 旁边、下方或上方的所有内容'.',并且我还尝试使用多个ifandfor循环来计算它,但没有任何运气。

 static void Main(string[] args)
        {
            // Variablen
            int intTestgeval = 0; // Number of times you want program to repeat 
            int intN = 0;         // Number of rows
            int intM = 0;         // Number of coloms
            char chrGrond;        // Used to store '*' or '.'
            char[,] chrRaster;    // 2d char array used to store all values 

            // Code
            try
            {
                intTestgeval = Int32.Parse(Console.ReadLine()); // Number of times program will repeat           

                if(intTestgeval > 150) // Program can not repeat more then 150 times 
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {                
                Environment.Exit(0);
            }

            intN = Controle(intN);                          // Number of rows ophalen
            intM = Controle(intM);                          // Number of Coloms ophalen

            chrRaster = new char[intN, intM];               // Initializing array with user input


            for (int intR = 0; intR < intTestgeval; intR++)  // Print 2d array to console
            {
                for(int intY = 0; intY < intN; intY++)
                {
                    for(int intZ = 0; intZ < intM; intZ++)
                    {
                        chrGrond = Convert.ToChar(Console.ReadKey().KeyChar);

                        chrRaster[intY, intZ] = chrGrond;
                    }
                    Console.WriteLine();
                }


                instorten[intR] = Instorten(chrRaster, intN, intM); // Ignore this part, that's another part of my assignment not related to my question.
            }
        }

        static int Controle( int intX )
        {
            try
            {
                intX = Int32.Parse(Console.ReadLine());

                if (intX > 150 || intX < 1) // Length of row and colom can not exceed 20 and can not go lower then 1
                {
                    throw new Exception();
                }
                return intX;
            }
            catch                           // Program will off if value does not meet requirements
            {
                Environment.Exit(0);
                return intX;
            }            
        }

  // It is this part of the program I need help with. This is what I tried but can't get any further
        static int Instorten(char[,] chrRaster, int intN, int intM)
        {
            for (int intY = 0; intY < intN; intY++)
            {
                for (int intZ = 0; intZ < intM; intZ++)
                {
                    if(chrRaster[intY, intZ] == '.' && chrRaster[intY, intZ + 1] == '*' || chrRaster[intY, intZ] == '*' && chrRaster[intY, intZ + 1] == '.')
                    {

                    }                    
                }
                Console.WriteLine();
            }
            int intm = 0;
            return intm;
        }        
    }

标签: c#arrays

解决方案


这是执行您想要的算法。我试图在评论中解释我的代码。输出将匹配您要查找的内容。

    static void Main(string[] args)
    {
        char STAR = '*';
        char DOT = '.';

        var input = new char[,]
        {
            { STAR,STAR,STAR,STAR,STAR},
            { STAR,STAR,DOT,STAR,STAR},
            { STAR,STAR,STAR,STAR,STAR},
            { STAR,STAR,STAR,STAR,DOT}
        };

        var output = new char[4, 5];

        // Copy each from input to output, checking if it touches a '.'
        for (int x = 0; x < 4; x++)
        {
            for (int y = 0; y < 5; y ++)
            {
                if (input[x, y] == STAR)
                {
                    var isDot = false;
                    // Check left
                    if (x > 0)
                        isDot = input[x - 1, y] == DOT;

                    // Check right
                    if (x < 3)
                        isDot = isDot || (input[x + 1, y] == DOT);

                    // Check above
                    if (y > 0)
                        isDot = isDot || (input[x, y - 1] == DOT);

                    // Check below
                    if (y < 4)
                        isDot = isDot || (input[x, y + 1]) == DOT;

                    output[x, y] = isDot ? DOT : STAR;
                }
                else
                {
                    output[x, y] = input[x, y];
                }
            }
        }

        // Print output
        for (int x = 0; x < 4; x ++)
        {
            for (int y = 0; y < 5; y ++)
            {
                Console.Write(output[x, y]);
            }
            Console.WriteLine();
        }

        Console.Read();
    }

推荐阅读