This is a simple lottery system, through the loop number added to the ArrayList, and then use the collections interface of the shuffle method to disrupt the number, and then use the random number to extract the prize, with the Remove method to remove the extracted number, The next draw continues, and the winning numbers are finally displayed on the running table. The specific code is as follows
PackageZuoye;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Random; Public classChoujiang {ArrayList<Integer>Number ; PublicArraylist<integer>deal () {if(number==NULL) { number=NewArraylist<integer>(); for(inti=1;i<200;i++) {number.add (i); }} collections.shuffle (number); return(number); } Public voiddisplay () {Random R=NewRandom (); intA=r.nextint (200); System.out.println ("First prize is:" +number.get (a) + "No."); Number.remove (a);
int k=199; Collections.shuffle (number); for(intj=1;j<=10;j++) {a=r.nextint (k); System.out.println ("Second prize is:" +number.get (a) + "No."); Number.remove (a); k--; } } Public Static voidMain (String[]args) {Choujiang g=NewChoujiang (); G.deal (); G.display (); }}
The results of the operation are as follows:
Java Jobs (6)