class Program
    {
        static void Main(string[] args)
        {
                       
            int[] sort ={ 9, 3, 1, 5, 7, 8, 0, 4, 6, 2 };
            Console.Write("排序前:");
            for (int i = 0; i < sort.Length;i++ )
            {
                Console.Write("{0} ", sort[i]);
            }
            bool IsSort = true;
            int temp = 0;
            while (IsSort)
            {
                IsSort = false;
                for (int i = 0; i < sort.Length - 1; i++)
                {
                    if (sort[i + 1] < sort[i])
                    {
                        temp = sort[i];
                        sort[i] = sort[i + 1];
                        sort[i + 1] = temp;
                        IsSort = true;
                    }
                }            }
            Console.Write("排序后:");
            for (int i = 0; i < sort.Length; i++)
            {
                Console.Write("{0} ", sort[i]);
            }
        }
    }
这样只是对已经确定好的int数组进行排序,如何调整为键盘输入一串数字呢
是否string s =Console.ReadLine();
            char[] char1 = s.ToCharArray()
那怎么在转为int[]呢
望大家帮小弟谢谢

解决方案 »

  1.   

    可以这样
    string str = Console.ReadLine();
    string[] s = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    int[] sort = new int[s.Length];
    try
    {
        for (int i = 0; i < s.Length; i++)
        {
            sort[i] = Convert.ToInt32(s[i]);
        }
    }
    catch { Console.WriteLine("输入有误"); } 
    //---排序
      

  2.   

    可以用 textbox 接收键盘输入的一串数字字符,如果数字都是一位的,点击一个“排序”按钮,然后在点击事件里把这串数字字符进行逐位分解,放到一个INT型的数组,就可以用上面的方法做排序了另一个办法是利用 javascript,也是用一个 text 接收键盘的输入,给 text 写个 onkeydown 的事件,也就是每按下一个数字,就在 javascript 里执行这个事件,读取 text 里的数字字符串,分解,排序,再放回 text 里
      

  3.   

    多谢!!
    有些地方还不能理解 刚接触(不好意思)。不过认真看了前辈的内容后得到启发
    string s=console.ReadLine();
    int[] sort=new int[s.length];
    foreach(int i in s.length)
    {
       sort[i]=convert.int32(s[i])-48;
       Console.Write("{0} ", sort[i]);
    }
    ...
    ...