gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:33 34 4
gpt4 key购买 nike

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

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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com