看看其他版块像Java、PHP都有很多文章,可是web前端开发这里啥都没有,或许是新开课程,或许是学的人少?今天我在这里写篇文章做一个抛砖引玉的作用,希望看到的人都积极把你们的代码也共享给我们大家,相互学习:) 自己水平有限,编写的或许不尽人意请勿喷! <script> function ball(){ var reds=[];//声明一个数组用来存放随机生成的红球 while(reds.length<6){//红球有6位,下标从0开始到5结束 //随机生成红球,保存到新声明变量red中 var red=Math.floor(Math.random()*33+1); for(var i=0;i<reds.length;i++){//遍历数组里面的红球 if(red==reds[i]){//并且不能重复 break; } //将生成的红球存入数组中 } i==reds.length&&reds.push(red);//短路逻辑,符合这个长度就压入,不符合就不去执行 } document.write("随机生成的红球依次为:"+reds+"<br/>"); function compare(a,b){ return a-b; } reds.sort(compare); document.write("经过排序后的红球依次为:"+reds+"<br/>"); var blue=Math.floor(Math.random()*16+1);//声明蓝球。但是有一个我们就不使用数组了,只要在范围内生成就好了,无需遍历 document.write("随机生成的蓝球为:"+blue+"</br>"); var balls=reds.join(",")+"+"+blue; document.write("本次随机挑选的双色球为:"+balls+"<br/>"); } ball(); </script> <script> function compare(a,b){ return a-b; } function balls(){ //大乐透是前区35个选5个,后区是12个选2个,写法和双色球有点区别 var fronts=[]; while(fronts.length<5){ var front=Math.floor(Math.random()*35+1); for(var i=0;i<fronts.length;i++){ if(front==fronts[i]){ break; } } i==fronts.length&&fronts.push(front); } document.write("随机大乐透前区号码为:"+fronts+"<br/>"); fronts.sort(compare); document.write("排序后的大乐透前区为:"+fronts+"<br/>"); var behinds=[]; while(behinds.length<2){ var behind=Math.floor(Math.random()*12+1); for(var n=0;n<behinds.length;n++){ if(behind==behinds[n]){ break; } } n==behinds.length&&behinds.push(behind); } document.write("随机大乐透后区号码为:"+behinds+"<br/>"); behinds.sort(compare); document.write("排序后的大乐透后区为:"+behinds+"<br/>"); var ball=fronts.join(",")+"+"+behinds.join(" "); document.write("最终本次随机大乐透为:"+ball+"<br/>"); } balls(); </script> <script> function Dball(){ //3D共从0~9里面选1个,共分为3组,出球顺序随机但是不允许排序,出球的不同对应的奖金就不同 var ball1=Math.floor(Math.random()*10+1); //document.write("第一位随机号码为:"+ball1+"<br/>"); var ball2=Math.floor(Math.random()*10+1); //document.write("第二位随机号码为:"+ball2+"<br/>"); var ball3=Math.floor(Math.random()*10+1); //document.write("第三位随机号码为:"+ball3+"<br/>"); document.write("3D试机号为:"+ball1+","+ball2+","+ball3); } Dball(); </script> |
双色球、大乐透、3D精选版!祝君好运(Javascript)
最新推荐文章于 2024-05-15 14:50:23 发布