Need Help with my ticket Program

• 05-01-2013, 07:26 PM
Gruff
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...
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);     } }```
• 05-01-2013, 08:16 PM
JosAH
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
• 05-01-2013, 08:25 PM
Gruff
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
• 05-01-2013, 09:25 PM
JosAH
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
• 05-02-2013, 05:59 AM
DarrylBurke
Re: Need Help with my ticket Program