红球篮球产生代码
public class TestD
{
//产生几组数据
public void getzhu( int num)
{
for (int m = ; m < num; m++)
{
getred();
getblue();
Console.WriteLine("第"+(m+).ToString()+"组数据红球: "+ToString());
red = new int[];
i = ;
b = ;
blue = new int[];
}
} int[] red = new int[];//6个红球
int[] blue = new int[];//一个篮球
int i = ;//红球数组索引
int b = ;//篮球数组索引
//生成红球
public void getred() {
bool flag = true;
do
{
int temp;
temp = getnum(flag,);
if (red.Contains(temp))
{
flag = true;
}
else {
red[i] = temp;
i++;
if (i > )
{
flag = false;
}
else
{
flag = true;
}
} } while (flag); }
//生成蓝球
public void getblue()
{
bool flag = true;
do
{
int temp;
temp = getnum(flag, );
if (blue.Contains(temp))
{
flag = true;
}
else
{
blue[b] = temp;
b++;
if (b > )
{
flag = false;
}
else
{
flag = true;
}
} } while (flag); }
//产生数字
private int getnum(bool ifreset,int zuida)
{ int inum=;
if (ifreset==true)
{
Random rd = new Random();
inum = rd.Next(, zuida); }
return inum;
}
//重写tostring方法
public override string ToString()
{
return red[].ToString() + "--" + red[].ToString() + "--" + red[].ToString() + "--" + red[].ToString() + "--" + red[].ToString() + "--" + red[].ToString() + " 篮球是:" + blue[].ToString();
} }
调用代码
//调用代码
TestD testd = new TestD();
testd.getzhu();//生成三组
结果图