【发布时间】:2022-01-22 06:15:42
【问题描述】:
我有这样的问题。
我对列表中的所有数字进行了排序,并将它们打印在 listBox 中。我从创建数组的 txt 文件中读取了数字。
我需要用户按以下顺序输入任何数字(我保存在变量“a”中)这些数字:
- 首先是小于 a 的数字
- 那么数字等于 a
- 最后是大数字。
并在 listBox 中全部打印出来。
...
float x;
if (float.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out x))
{
lst.Items.Add(x);
}
List<float> array = new List<float>();
array.Add(x);
a = Convert.ToInt32(txt1.Text);
int at = lst2.Items.Count;
for (int o = 0; o < lst2.Items.Count; ++o)
{
if (x < (float)(lst2.Items[o]) && a >= o)
{
at = o;
break;
}
}
lst2.Items.Insert(at, x);
使用此代码,我只对数字进行排序,而不使用变量进行排序。
【问题讨论】:
-
first the numbers less than a, then the numbers equal to a, and finally the big numbers- 听起来像是一个不涉及任何a变量的命令。 -
@GSerg 可能“小于 a 的数字”和“大数字”可以不排序。
-
@Dmitry 给定
I sorted all the numbers in the list,顺序相同。 -
不不。我明白问题的第二部分似乎毫无意义,但实际上所有的数字都会被排序,我明白了(数字从小到大排列),但是还需要用户输入的值(这将只是 1 个数字)。包含在该排序中
-
这能回答你的问题吗? How to insert item into list in order?