C#——将输入的n个数字,通过for语句排序并输出。 注意,不允许使用Array.Sort()方法排序

首先设计如下界面:

编写如下代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace c

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        Double [] tj=new Double [100];

        int num = 0;

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                tj[num] = Convert.ToDouble(textBox1.Text);

                label2.Text += tj[num] + "  ";

                num++;

                textBox1.Text = "";

            }

            catch 

            {

                MessageBox.Show("请正确输入数字");

            }

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            label2.Text = "排序前的序列:";

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            label2.Text += "\n排序后的序列:";

            double t = 0;

            for (int i = 0; i < num-1;i++ )

            {

                for (int j = i+1; j < num;j++ )

                {

                    if(tj[j]<tj[i])

                    {

                        t = tj[j];

                        tj[j] = tj[i];

                        tj[i] = t;

                    }

                }

            }

            for (int s = 0; s < num;s++ )

            {

               label2.Text += tj[s]+"  ";

            }

        }

    }

}

 

运行结果:

猜你喜欢

转载自blog.csdn.net/lmm0513/article/details/88773359