我用C#写了一个福彩双色球程序,关键代码如下:
public void CreatRandomNum()
{
Hashtable hashtable = new Hashtable();
Random random = new Random();
int numCount = 6;
for (int i = 0;hashtable.Count<numCount;i++)
{
int nValue=random.Next(34);
if(!hashtable.ContainsValue(nValue)&&nValue!=0)
{
hashtable.Add(nValue,nValue);
this.txtRedBall.Text+=nValue.ToString()+"   ";
}
}
this.txtBlueBall.Text=random.Next(16)+1+"";
} private void btnOpen_Click(object sender, System.EventArgs e)
{
this.txtRedBall.Text="";
this.txtBlueBall.Text="";
CreatRandomNum();
} private void btnClear_Click(object sender, System.EventArgs e)
{
this.txtRedBall.Text="";
this.txtBlueBall.Text="";
} private void btnExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
现在想对显示在txtRedBall中的数字从小到大排序,不会搞了,请各位帮一下忙,谢谢!

解决方案 »

  1.   

    把this.txtRedBall.Text的值传到int[6]数组中,
    再对数组排序,冒泡法等都行。
      

  2.   

    List<Int16> list = new List<short>();
            Random random = new Random();
            int numCount = 6;
            for (int i = 0; list.Count < numCount; i++)
            {
                int nValue = random.Next(34);
                if (!list.Contains(nValue)&& nValue != 0)
                {
                    list.Add(nValue);
                }
            }
            list.Sort();
            string text = string.Empty;
            foreach (Int16 x in list)
            {
                text += x.ToString();
            }
    this.txtRedBall.Text = text;
      

  3.   

    将hashtable中的数字输出到arraylist中然后排序 再输出至textbox
      

  4.   

    LZ用HashTable功能好像就只体现在了判断给数字是否已经存在了,
    那干嘛还用HashTable呢,
    我上面那段代码不是也可以吗,用范型的List,可以直接排序,然后再输出来
      

  5.   

    想请问一下Framework 1.1的需要导入哪个命名空间才能用范型的List啊?
      

  6.   

    范型的List是2.0的东西.导入哪个命名空间也不能用
      

  7.   

    将hashtable中的数字输出到arraylist中然后排序 -->这个可行的
      

  8.   

    如果不嫌弃 这里有个麻烦点的。
                Hashtable ht = new Hashtable();
                int[] uRand = GetUniqueRandom();//获得随机数 随便写写,呵呵
                int index = 0;
                foreach(int rand in uRand)
                {
                    index ++ ;
                    ht.Add(index, rand);
                }
                ArrayList al = new ArrayList(ht);
                IComparer myComparer = new myComparerClass();
                al.Sort(myComparer);            IEnumerator objEnum = ((IEnumerable)al).GetEnumerator();
                int i = 0;
                while (objEnum.MoveNext())
                {
                    Object obj = objEnum.Current;
                    Console.WriteLine("\t[{0}]:\t{1}", (Int32)i++, ((DictionaryEntry)obj).Value);
                }////////////////////////////////////////////////////
    //下边这个就是比较的接口实现
            public class myComparerClass : IComparer
            {
                // 比较Value,不比较key
                int IComparer.Compare(Object x, Object y)
                {
                    DictionaryEntry X = (DictionaryEntry)x;
                    DictionaryEntry Y = (DictionaryEntry)y;
                    if ((int)X.Value == (int)Y.Value)
                        return 0;
                    else if ((int)X.Value > (int)Y.Value)
                        return 1;
                    else
                        return -1;
                }
            }
      

  9.   

    稍稍简化一下
    List<string> list = new List<string>();
    Random random = new Random();
    int numCount = 6;
    for (int i = 0; list.Count < numCount; i++)
    {
        int nValue = random.Next(1,34);
        if (!list.Contains(nValue))
        {
            list.Add(nValue.ToString());
        }
    }
    list.Sort();
    string text = string.Join(" ",list.ToArray())
    this.txtRedBall.Text = text;