【问题标题】:Assign values to another position [closed]将值分配给另一个位置[关闭]
【发布时间】:2015-04-22 18:10:03
【问题描述】:

我有一个非常简单的问题。但是我遇到了麻烦。

我有下面的 for 循环。我需要分配一些值。

int i;
for (i = currPos + 1; i < expPos;i++ )
{
    [i + 1] = i;// I want to assign i th position to the i+1 
}

【问题讨论】:

  • [i + 1] 是什么意思?你有一个数组或其他地方的东西吗?能不能说的具体点?
  • 你需要递减i吗?这是您要查找的内容:for (i = currPos + 1; i &lt; expPos; i-- )?注意i-- 而不是i++
  • 正如 Soner 已经说过的,我很乐意提供有关代码的更多详细信息(您有数组,它是什么类型的?)。你想用array[i+1]'s 交换array[i] 的值吗?如果这就是您要查找的内容,则需要一个临时对象,如下所述:stackoverflow.com/questions/3614785/…

标签: c# asp.net asp.net-mvc


【解决方案1】:

我相信您想要以下内容:

//You would want to create a custom Player class to store this data as it looks silly here but:
        string[] objectNames = { "player1", "player2", "player3", "player4" };
        int[] objectPositionX = { 5, 10, 15, 20 };
        int[] objectPositionY = { 50, 60, 70, 70 };
        for (int i = 0; i < objectNames.Length; i++)
        {
            //An example of this use would be teleporting a player to another player? Say player 2 to player 4's location.
            if(objectNames[i] == "player2")
            {
                objectPositionX[i] = objectPositionX[3];
                objectPositionY[i] = objectPositionY[3];
            }
        }

        //I'm only taking a stab at what you want as the question was a tad vague. But hope this helps.

【讨论】:

    【解决方案2】:

    如果我理解正确的话。假设你有一个名为“A”的数组并且你想设置A[i+1]=A[i];的位置 但请注意丢失[i+1] 索引的数据。 任何方式请更具体。

    【讨论】:

    • 是的,我有一个数组,你的理解是正确的。
    • @Milen .. 感谢您更新我的答案。
    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 2023-02-22
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多