C#写的简单的彩票摇号系统。

我想实现的是一个按键控制出7个号码。要求就只用一个TIMER控件,然后号码不能重复,不能是伪随机数,求详细代码。

第1个回答  2017-05-31
//写得有点简单了 ,大神勿喷,有更好的方法求教 ,
public class Lottery {
public static void main(String[] args) {
//开始时间
long start =System.currentTimeMillis();
//开奖集合
List<Integer> numbers = new ArrayList<Integer>();
//彩票数组
int[] balls = new int[]{5,7,16,20,21,25,05};
//中奖个数标记
int k = 0;
//执行次数
int times = 0;
do {
//先生成前6个中奖号码
numbers.addAll(creatRandomList());
//对比前6个号码 ,中一个K值加1
for(int j=0 ;j<balls.length-1;j++){
int ball = balls[j]
;
if(numbers.contains(ball)){
k++;
}
}
//对比篮球 中了 K值加1
int endBall = new Random().nextInt(16) + 1;
if(balls[balls.length-1]==endBall){
k++;
}
//如果没中一等奖 ,将K值恢复为0
if(k<7){
k=0;
}
numbers.add(endBall);
System.out.println("开奖号码:"+numbers);
//清空集合
numbers.clear();
//执行次数加1
times++;
}while (k<7);
System.out.println("一共开奖次数:"+times);
long end =System.currentTimeMillis();
System.out.println(end-start);
}
//产生 6个随机数
public static List<Integer> creatRandomList(){
List<Integer> numbers = new ArrayList<Integer>();
int i= 0 ;
int number ;
do{
number= new Random().nextInt(33) + 1;
if(!numbers.contains(number)) {
numbers.add(number);
i++;
}
}while (i<6);
return numbers ;
}
}
第2个回答  2014-01-24
List<int> filters = new List<int>();
private Timer timer;
private Random random = new Random();
public Form1()
{
InitializeComponent();
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
Start();
}
private void Start()
{
all.Clear();
filters.Clear();
for (int index = 1; index <= 36; index++)
{
all.Add(index);
}
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
var index = random.Next(0, all.Count-1);
var value = all[index];
filters.Add(value);
all.Remove(value);
if (filters.Count == 7)
{
timer.Stop();
}
}

如果需要从大到小排序,那就在timer.Stop();之后加个排序就好了追问

这个all是什么东西?~

追答

存放所有号码的集合名称

追问

这个需要定义么??~我用VS为啥报错呐。~

追答

List all = new List();
List filters = new List();
private Timer timer;
private Random random = new Random();

一开始复制的时候少给你复制了第一行。

追问

这不是我想要的效果阿,我想要的效果是在窗体下面用一个按键触发可视的随机数选项,然后暂停的时候全部数字再一起停下,就跟那些电视上抽奖一样,先滚动,然后再停止。这样子。~~
就是用了8个label来显示数字。~36选7个数。~

追答

那你就加个按钮事件
开始选的时候调 Start();
暂停的时候,把filters里的数显示到界面。
顺便把timer.Interval = 1000;
改成timer.Interval = 100;

本回答被提问者和网友采纳
第3个回答  2014-01-24
设置一下随机因子,或者自己写个算法判断一下就行

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网