繁体   English   中英

如何将在文本框中键入的数字添加到数组中,以及如何从最大到最小排序?

[英]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?

我想知道如何在文本框中添加或键入数字,然后保存该数字,然后添加其他数字并保存它们,因此最后我可以按从大到小的顺序对它们进行排序。

我得到一个文本框(在其中键入数字),一个按钮(添加按钮,将键入的数字添加到textbox2),另一个文本框2(在其中同时添加数字,以便您可以对其进行检查)。 有一个textbox3(数字必须按从大到小的顺序显示)和一个textbox4(数字必须按从小到大的顺序显示)。

有人能帮我吗?

并不完美,但可以。 试试吧。:)

    //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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM