General idea:
Add the number to the ArrayList dynamic array, use the static method of the set collections.shuffle () the elements in the sequence set to get the random number,remove Delete a drawn number
The code is as follows:
1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 ImportJava.util.Random;4 5 Public classRaffle {6 7 PrivateArraylist<integer>list;8 9 Private voiddeal () {Ten //adds a specified number of num integers to the list container in order One if(list==NULL) { AList =NewArraylist<integer>(); - for(inti = 1; i < 1000; i++) { - List.add (i); the } - } - //To disrupt the order of elements in a list - collections.shuffle (list); + } - + //Lottery Method: Draw a specified number of prizes A Public voidDraw () { atRandom Rdom =NewRandom (); - - intindex =Rdom.nextint (List.size ()); -System.out.println ("First prize:" +List.get (index)); - List.remove (index); - collections.shuffle (list); in - to for(inti = 0; I < 2; i++) { + intIndex2 =Rdom.nextint (List.size ()); -System.out.println ("Second Prize:" +list. Get (INDEX2)); the List.remove (INDEX2); * } $ collections.shuffle (list);Panax Notoginseng - the for(inti = 0; I < 3; i++) { + intIndex3 =Rdom.nextint (List.size ()); ASystem.out.println ("Third prize:" +list. Get (INDEX3)); the List.remove (index3); + } - collections.shuffle (list); $ } $ - - Public Static voidMain (string[] args) { the //TODO auto-generated Method Stub -Raffle RF =NewRaffle ();Wuyi rf.deal (); the Rf.draw (); - Wu } -}
Operation Result:
Java (vi)--lottery system