Results 1 to 5 of 5
  1. #1
    Gruff is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Question Need Help with my ticket Program

    Alright so I am still kinda new to java

    I am working on a lottery program that will ask the user how many tickets they would like (default being five) and each ticket has a random set of numbers plus a jackpot number:

    this is the code have so far...
    Java Code:
    import javax.swing.JOptionPane;
    import java.util.Arrays;
     
    public class LuckyNumbers 
    {
        public static void main(String[] args) 
        {
            // number of lucky numbers
            // number of elements for the lucky numbers
            final int LUCKY_NUMBERS = 5;     
            int[] luckyNumbers = new int[LUCKY_NUMBERS];       
                   
            // generate five random numbers
            for (int i = 0; i < luckyNumbers.length; i++) 
            {
                luckyNumbers[i] = (int) (Math.random() * 56) + 1;
            }
                   
            Arrays.sort(luckyNumbers); // sort the elements
                   
            int goldenNumber = (int) (Math.random() * 46) + 1;  // draw the golden number
            
            // print the numbers
            JOptionPane.showMessageDialog(null, "Your Lucky Numbers arrrrre....\n*drumroll*\n\n" 
            + "Ticket 1:\n" + Arrays.toString(luckyNumbers) + " Golden Number:" + goldenNumber, 
            "WELCOME TO THE LOTTERY!",
            JOptionPane.INFORMATION_MESSAGE);
        }
    }
    thanks for any help you can give
    Last edited by Gruff; 05-01-2013 at 08:47 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Need Help with my ticket Program

    Shouldn't those five numbers all be different? Note that the random number generator can generate the same numbers in a sequence.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Gruff is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: Need Help with my ticket Program

    yeah I forgot about that x.x i was having a bit of a problem with that also, due to not knowing how to keep it from generating the same numbers

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Need Help with my ticket Program

    The collections utility class has a shuffle( ... ) method; if you have a List (<--- a Collection) that contains the numbers 1 ... 56 you can shuffle it and take the first five numbers from it and the sixth number can be the 'golden number'; if you do it right it'll be just a few lines of code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Need Help with my ticket Program

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  2. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  3. SCJP ticket for Sale
    By nphariharan in forum Java Certification
    Replies: 6
    Last Post: 07-14-2009, 11:38 AM
  4. Require SCJP 1.6 exam ticket
    By nphariharan in forum Java Certification
    Replies: 0
    Last Post: 07-12-2009, 07:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •