首页 > 解决方案 > 二维数组中的移动元素

问题描述

我可以将元素“kanin”从维度 0 移动到 1,但不能使用我的 else 语句返回。我的目标是检查这个“元素”是否在维度 0 中,它移动到维度 1,如果它在维度 1 中,它移动到维度 0。但只有 if 语句有效

public static string[,] Kanin(string[,] strand) {
        for (int i = 0; i < strand.GetLength(0); i++)
        {
            if (i==0)
            {
                for (int x = 0; x < strand.GetLength(1); x++)
                {
                    if (strand[i, x] == "kanin")
                    {

                        for (int y = 0; y < strand.GetLength(1); y++)
                        {
                            if (strand[1, y] == "")
                            {
                                strand[1, y] = strand[i, x];
                                strand[i, x] = "";
                            }
                        }
                    }
                }
            }
            
            else
            {
                for (int x = 0; x < strand.GetLength(0); x++)
                {
                    if (strand[i, x] == "kanin")
                    {
                        for (int y = 0; y < strand.GetLength(0); y++)
                        {
                            if (strand[0, y] == "")
                            {
                                strand[0, y] = strand[i, x];
                                strand[i, x] = "";
                            }
                        }
                    }
                }
            }
            

        }

        return (strand);
    }

标签: arraysfor-loopif-statementmultidimensional-array

解决方案


推荐阅读