Java (vi)--lottery system

Source: Internet
Author: User
Tags shuffle

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.