Java implementation of color ball machine selection algorithm
One, the code
PackageCom.hdwang;ImportJava.util.Random;/*** Created by admin on 2017/1/10. * Color ball Machine Selection Implementation*/ Public classSSQ { Public Static voidMain (string[] args) {intArraynums = 5;//Machine Selection 5 Group for(inti=0;i<arraynums;i++) {String HM=Getyzhm (); SYSTEM.OUT.PRINTLN (HM); } } /*** Generate a set of color ball *@returna note number*/ Private StaticString Getyzhm () {//33 Select 6 int[] exist =New int[6];//default Full 0 for(inti=0;i<6;i++){ intBall = Getaball (33, exist); Exist[i]= Ball;//staging a selected ball} sort (exist); //Sort//16 Select 1 intSpecialball = Getaball (16,NULL); //Stitching StringsString HM = ""; for(inti=0;i<6;i++) {String num= Exist[i] < 10? "0" +exist[i]: "" +Exist[i]; if(i==0) {HM+=num; }Else{HM+= ","+num; }} HM+ = "\ T" + (specialball<10? "0" +specialball: "" +Specialball); returnHM; } /*** Shake out a number *@paramtotal number of balls *@paramexist has chosen the ball *@returna new number*/ Private Static intGetaball (intTotalint[] exist) {Random random=NewRandom (); intBall = Random.nextint (total) +1; while(true){ if(contains (Exist,ball)) { Ball= Random.nextint (total) +1; }Else{ Break;//take the new ball and end it } } returnBall ; } /*** Determine if an array contains an element *@paramArray Arrays *@paramvalue Element *@returnwhether there is*/ Private Static BooleanContainsint[] Array,intvalue) { Booleanc =false; if(Array = =NULL){ return false; } for(inti=0;i<array.length;i++){ if(Array[i] = =value) {C=true; Break; } } returnC; } /*** Sort *@paramArray Arrays*/ Private Static voidSortint[] Array) { for(inti=0;i< array.length-1;i++) {//than how many times for(intJ= i+1;j<array.length;j++) {//each cycle, the minimum number is advanced if(Array[i]>array[j]) {//Fractional Bubbles intTMP =Array[i]; Array[i]=Array[j]; ARRAY[J]=tmp; } } } }}
Ii. Results
07,09,12,21,28,32 14
13,18,19,27,31,3212
01,02,05,16,19,2514
01,04,06,19,23,3316
09,10,11,16,28,3310
Java implementation of color ball machine selection algorithm