[英]How do I add a number that I type in a textbox to an array, and how can I orde ir from the largest to the smallest?
I would like to know how can I add or type a number in a textbox, then this number gets saved, then add other numbers, and save them so at the end I can order them from larger to smaller and viceversa. 我想知道如何在文本框中添加或键入数字,然后保存该数字,然后添加其他数字并保存它们,因此最后我可以按从大到小的顺序对它们进行排序。
I got one textbox (where I type the numbers), one button (add button, that adds the typed number to the textbox2), another textbox2(where the numbers are being added simultaneously, so you can check them). 我得到一个文本框(在其中键入数字),一个按钮(添加按钮,将键入的数字添加到textbox2),另一个文本框2(在其中同时添加数字,以便您可以对其进行检查)。 There is a textbox3 (where the numbers must appear ordered from larger to smaller) and a textbox4 (where the numbers must appear ordered from smaller to larger).
有一个textbox3(数字必须按从大到小的顺序显示)和一个textbox4(数字必须按从小到大的顺序显示)。
Can someone help me? 有人能帮我吗?
Not perfect but it works. 并不完美,但可以。 Try it.:)
试试吧。:)
//index count
int index=0;
//array declaration
string [] numbers=new string[10];
//method displaying array's content
string arrayDisplay() {
string str="";
for (int i = 0; i < numbers.Length; i++)
{
if (!(numbers[i]== "") )
{
str += numbers[i];
}
}
return str;
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text += textBox1.Text;
index++;
if (numbers.Length >=index )
{
numbers[index] = textBox1.Text;
textBox1.Text = "";
}
//Regular sort and display
Array.Sort(numbers);
textBox3.Text = arrayDisplay();
//Reverse sort and display
Array.Reverse(numbers);
textBox4.Text = arrayDisplay();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.