c# - 输出不是在同一数组中交换元素的预期输出

标签 c# arrays element swap

我创建了一个示例数字列表以存储在数组中,我想交换同一列中的元素。

1.(200,-400)
2.(300,-6000) 
3.(400,-125)
4.(100,-120) 

The first group of number will swap with the second group of number making 1.(300,-6000) 2.(200,-400) 3.(400,-125) 4.(100,-120) then second group of numbers will swap with the third group of numbers becoming 1.(300,-6000) 2.(400,-125) 3.(200,-400), and this goes till the last array.

However I am not getting the output I am suppose to get... Can someone tell me whats wrong... Thank you.

private void swapButton_Click(object sender, EventArgs e)
{
    double[] b1x = new double[4];
    double[] b1y = new double[4];
    double[] b2x = new double[4];
    double[] b2y = new double[4];
    double[,] array = new double[,]
    {
        {200,-400},
        {300,-6000},
        {400,-125},
        {100,-120}
    };

    for (int x =0; x < 4; x++)
    {
        b1x[x] = array[x, 0];
        b1y[x] = array[x, 1];

        for (int y = x+1; y < 4; y++)
        {
            b2x[x] = array[y, 0];
            b2y[x] = array[y, 1];
        }
        array[x, 0] = b2x[x];
        array[x, 1] = b2y[x];

        for (int w = x+1; w < 4; w++)
        {
            array[w, 0] = b1x[x];
            array[w, 1] = b1y[x];
        }
    }           
}

最佳答案

我已经编写了这个控制台应用程序,可能这就是您要找的东西。这将在每个步骤中打印带有交换值的数组。

        double bx, by;

        double[,] array = new double[,]
        {
        {200,-400},
        {300,-6000},
        {400,-125},
        {100,-120}
        };

        for (var i = 0; i < 4; i++)
        {
            Console.Write($"({array[i, 0]}, {array[i, 1]})");
        }

        for (int x = 0; x < 3; x++)
        {

            bx = array[x, 0];
            by = array[x, 1];

            array[x, 0] = array[x + 1, 0];
            array[x, 1] = array[x + 1, 1];

            array[x + 1, 0] = bx;
            array[x + 1, 1] = by;

            Console.WriteLine();

            for (var i = 0; i < 4; i++)
            {
                Console.Write($"({array[i, 0]}, {array[i, 1]})");
            }

        }

        Console.ReadKey();

关于c# - 输出不是在同一数组中交换元素的预期输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222634/

相关文章:

selenium - 在页面弹出窗口中找不到带有定位器的元素

c# - 获取字符串数组中匹配元素的所有索引

c# - ManyToOne 关系上的 Xamarin.Forms Sqlite-net NotSupportedException "Don' 不了解 <model>"

c# - 可访问性不一致 : property type

c - 为什么我的程序接受的整数太多而输入的整数太少?

java - 将对象数组转换为字符串数组?

javascript - 按两个不同的标准对数组中的对象进行排序

javascript - 创建元素有哪些不同的方式

c# - 使用 NancyFx 的好处?

html - 刷新时元素消失