【发布时间】:2010-12-18 15:45:39
【问题描述】:
这段代码给出了输出,但它有一个问题是当用户在 textbox1 中写入 5,6 和在 textbox3 中写入 7,8 时,它输出 5,6。我知道问题是当数组的元素结束时,它没有打印其他数组的其余元素,我评论了问题行。
已编辑:我使用 textbox1 和 textbox3 来获取用户想要合并的数组元素
private void button3_Click(object sender, EventArgs e)
{
string[] source = textBox1.Text.Split(',');
string[] source1 = textBox3.Text.Split(',');
int[] nums2 = new int[8];
int[] nums = new int[source.Length];
for (int i = 0; i < source.Length; i++)
{
nums[i] = Convert.ToInt32(source[i]);
}
int[] nums1 = new int[source1.Length];
for (int j = 0; j < source1.Length; j++)
{
nums1[j] = Convert.ToInt32(source1[j]);
}
int x = 0;
int y = 0;
int z = 0;
while (x < nums.Length && y < nums1.Length)
{
if (nums[x] < nums1[y])
{
nums2[z] = nums[x];
x++;
}
else
{
nums2[z] = nums1[y];
y++;
}
z++;
}////----->>it works untill here
while (x > nums.Length)///this mean when the elements of nums end,out the rest of the elements in other textbox but it doesnt do anything,whats the problem ?
{
if (y <= nums1.Length)
{
nums2[z] = nums1[y];
z++;
y++;
}
}
while (y > nums1.Length)
{
if (x <= nums.Length)
{
nums2[z] = nums[x];
z++;
x++;
}
}
string merge = "";
foreach (var n in nums2)
merge += n.ToString() + ",";
textBox4.Text = merge;
}
【问题讨论】:
-
合并排序,文本框,你在说什么?合并排序中没有臭的 texbox!
-
我使用 textbox1 和 textbox3 来获取用户想要合并的数组元素
-
不,这并不比您之前重复的问题更清楚:stackoverflow.com/questions/4477248/…
-
@Cody Gray:在最后一个问题中,我的输出是 0,但现在我没有零