【问题标题】:Swapping values of two variables交换两个变量的值
【发布时间】:2015-03-18 14:11:41
【问题描述】:

我目前很难交换两个变量。一旦用户输入空白单元格旁边的值,我希望能够交换值 为我非常混乱的代码道歉,我只是在学习 C#。

static void SwapNums(string[,] theBoard)
    {
        int col, row;
        string swap;
        string number = ReadNumber();

        for (col = 0; col                 if (theBoard[col, row] == "")
                            {
                                theBoard[col, row] = number;

                            }
                        }
                    }                            

                }
            }
        }
    }
} < 6; col++)
        {
            for (row = 0; row < 6; row++)
            {
                if (theBoard[col,row] == number)
                {
                    if (theBoard[col + 1, row] == "-" || theBoard[col - 1, row] == "-" || theBoard[col, row + 1] == "" || theBoard[col, row - 1] == "-")
                    {
                        swap = theBoard[col, row];

                        theBoard[col, row] = "";

                        for (col = 0; col < 6; col++)
                        {
                            for (row = 0; row < 6; row++)
                            {
                                if (theBoard[col, row] == "")
                                {
                                    theBoard[col, row] = number;

                                }
                            }
                        }                            

                    }
                }                 if (theBoard[col, row] == "")
                            {
                                theBoard[col, row] = number;

                            }
                        }
                    }                            

                }
            }
        }
    }
}
            }
        }
    }

目前,此代码正在将空白单元格替换为用户输入的内容,但不会将包含数字的单元格替换为 p。

【问题讨论】:

  • 不清楚你在问什么。您的示例有 5 列,在您的代码中有 7 列。如果用户输入“3”,应该会发生帽子?
  • 一些提示:首先在纸上做,用手,观察你在做什么。然后想想代码、变量、结构、算法。然后编码。然后使用调试器查看您的代码实际做了什么。
  • 不需要if语句内部的嵌套for循环。
  • @DrKoch 对不起,这个例子展示了它的样子。如果用户输入 3,则不会发生任何事情,因为该数字不在“-”旁边。
  • if (theBoard[col + 1, row] == "-" || theBoard[col - 1, row] == "-" || theBoard[col, row + 1] = = "-" || theBoard[col, row - 1] == "-") 告诉您哪个单元格包含“-”,但是将所有逻辑检查放在一起会掩盖它是哪个单元格。

标签: c# variables swap


【解决方案1】:

创建一个获取元素“位置”的函数。像这样?

const int ROWS = 6;
const int COLUMNS = 6;

static Tuple<int, int> GetPosition(string[,] theBoard, string value)
{
    for (int i = 0; i < ROWS; i++)
        for (int j = 0; j < COLUMNS; j++)
            if (theBoard[i, j] == value)
                return new Tuple<int, int>(i, j);

    return new Tuple<int, int>(-1, -1);
}

然后,只需交换元素,如下所示:

var numberPosition = GetPosition(theBoard, number);
var minusPosition = GetPosition(theBoard, "-");

theBoard[numberPosition.Item1, numberPosition.Item2] = "-";
theBoard[minusPosition.Item1, minusPosition.Item2] = number;

确保检查是否找到了元素! (Item1Item2 将是 -1 如果不是)

给你,说明概念的完整代码:http://pastebin.com/5kjDPeX8

编辑:

哦,是的,只有当元素在它旁边时才应该交换它,所以,只需检查返回的位置即可。这是SwapNums方法的替换:(我没有更新上面的pastebin代码)

static void SwapNums(string[,] theBoard, string number)
{
    var numberPosition = GetPosition(theBoard, number);
    var minusPosition = GetPosition(theBoard, "-");

    if (numberPosition.Item1 == -1 || minusPosition.Item1 == -1)
        throw new Exception("Element " + number + " or - was not found in theBoard!");

    if (numberPosition.Item1 == minusPosition.Item1) //they are in the same row
    {
        if (numberPosition.Item2 + 1 == minusPosition.Item2 ||
            numberPosition.Item2 - 1 == minusPosition.Item2) // if they are next to eachother
        {
            theBoard[numberPosition.Item1, numberPosition.Item2] = "-";
            theBoard[minusPosition.Item1, minusPosition.Item2] = number;
        }
    }
    else if (numberPosition.Item2 == minusPosition.Item2) // same column
    {
        if (numberPosition.Item1 + 1 == minusPosition.Item1 ||
            numberPosition.Item1 - 1 == minusPosition.Item1) //if they are above or below
        {
            theBoard[numberPosition.Item1, numberPosition.Item2] = "-";
            theBoard[minusPosition.Item1, minusPosition.Item2] = number;
        }
    }
}

稍微题外话(可能具有教育意义):

那个Tuple&lt;int, int&gt;的东西只是一个包含两个元素的类(即int Item1int Item2),当你的函数需要返回两个东西(在我们的例子中是行和列元素的位置)。

&lt;int, int&gt; 部分表示Item1Item2 的类型将为int。类上的&lt;something something etc.&gt; 事物通常是称为generics 的事物的一部分,这是一种高级编程概念。

简而言之(关于泛型),它允许您创建“通用”类型的对象,它可以操作不同类型的对象。 Tuple 这里可以包含任何类型的对象对; Tuple&lt;string, int&gt; 将有 string Item1int Item2

但这不是你现在应该担心的事情。当您自己制作了一些课程后,您就会明白为什么这样做很方便。目前,Tuple 类是您需要从函数中快速轻松地返回 2 个东西的东西。

【讨论】:

  • @Viroe 嘿,有一个更新...我应该开始阅读问题了 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多