红球1——33号,篮球1——16号,随机产生6红1篮,请问用c#如何编写?
红球1——33号,篮球1——16号,随机产生6红1篮,请问用c#如何编写?
2011-04-13 22:08
程序代码:using System;
public class BlueRed
{
public static void Main()
{
Random rd=new Random(DateTime.Now.Millisecond);
int blue=rd.Next(1,16); //生成一个篮球
int []red=new red[6];
int count=0;
while(count!=6) //红球生成6个不同的随机数
{
int j=rd.Next(1,33);
if(!NumExists(j,red)) //检查生成的随机数是否重复
{
red[count]=j;
count++;
}
}
Console.WriteLine(blue.ToString()); //输出篮球
foreach(int x in red) //输出红球
{
Console.Write(x.ToString()+"\t");
}
Console.WriteLine();
Console.ReadKey();
}
public bool NumExists(int a,params int []m)
{
foreach(int x in m)
{
if(x==a)
{
return true;
}
}
return false;
}
}

2011-04-13 23:55
2011-04-14 00:12
2011-04-14 00:16
程序代码:using System;
public class BlueRed
{
public static void Main()
{
Random rd = new Random(DateTime.Now.Millisecond);
int blue = rd.Next(1, 16); //生成一个篮球
int[] red = new int[6];
int count = 0;
while (count != 6) //红球生成6个不同的随机数
{
int j = rd.Next(1, 33);
if (!NumExists(j, red)) //检查生成的随机数是否重复
{
red[count] = j;
count++;
}
}
Console.WriteLine(blue.ToString()); //输出篮球
foreach (int x in red) //输出红球
{
Console.Write(x.ToString() + "\t");
}
Console.WriteLine();
Console.ReadKey();
}
public static bool NumExists(int a, params int[] m)
{
foreach (int x in m)
{
if (x == a)
{
return true;
}
}
return false;
}
}


2011-04-14 00:32
2011-04-14 09:22
2011-04-28 02:21
2011-04-28 06:17