我用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中的数字从小到大排序,不会搞了,请各位帮一下忙,谢谢!
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中的数字从小到大排序,不会搞了,请各位帮一下忙,谢谢!
解决方案 »
- DataGridView中绑定CheckBox(超级变态级)
- Graphics.FromImage()与Bitmap的问题
- 一道C#中实现ICloneable 接口深拷贝的问题
- Winform+D8
- c#写的windows form程序怎么样可以实现像WINDOWS服务一样,无需登陆WINDOWS用户即可自动启动?
- Messbox.show的用法
- DataTable.Clear()和DataTable.Rows.Clear()引发的问题
- 问一个和xml有关的简单问题?
- 泛型怎么用啊?
- 在access的文本中如何比较数字大小?
- 怎么让javascript控制页面是否提交?
- Remoting中,怎么测试对象生命周期?
再对数组排序,冒泡法等都行。
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;
那干嘛还用HashTable呢,
我上面那段代码不是也可以吗,用范型的List,可以直接排序,然后再输出来
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;
}
}
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;