Results 1 to 3 of 3
  1. #1
    jib123 is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default lottery GUI program

    Ive created an operating GUI lottery program which should generate 6 random numbers between 0-49. The only problem i have is that it doesnt do so. I have only been given this program to create 3 days ago and have to hand it in next week.

    At the moment the program keeps generating the numbers 1,2,3,4,5,6 whenever i press on the button to generate. I know this may be a fairly easy fix to some but i have looked and relooked at my code and cant see whats wrong.

    lottery GUI program-lotto-nums.jpg

    Java Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener; 
    
    public class lotteryGenerator implements ActionListener
    
    {       
            private JFrame frame = new JFrame("Lottery Generator Program");
            private JPanel panel = new JPanel(); 
            private JButton generateLottoNums = new JButton("Generate Numbers");
            private String lottoNums = "";
            private JTextField lotteryText = new JTextField(20);
            ImageIcon Lottery = new ImageIcon("Lottery.png");
            private JLabel label = new JLabel(Lottery);
                 
            
            public static void main (String[] args)
            {
                lotteryGenerator gui = new lotteryGenerator();
            }
            
            public void lotteryGenerator()
            {
               //Defining the Window
                frame.add(panel);
                frame.setSize(500, 300);
                frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                generateLottoNums.addActionListener(this);
               
                //Adding the button, label and image to the panel
                panel.add(label);
                panel.add(generateLottoNums);
                panel.add(lotteryText);
            }
            
            int[] lotteryGenerator = new int[50];
            int i = 0;
            Random rgen = new Random();
            
            //Creating an Array of 50 integers
            public void createNumbers()  
            {
                //Creating an Array of 50 Integers            
                for (i=0; i < lotteryGenerator.length; i++) 
                {   
                    lotteryGenerator[i] = i + 1;
                }
            }
            
            public void shuffleNumbers()
            {
                // Shuffling the numbers in the array
                for (int i=0; i < lotteryGenerator.length; i++) 
                {
                    int randomPosition = rgen.nextInt(lotteryGenerator.length);
                    int temp = lotteryGenerator[i];
                    lotteryGenerator[i] = lotteryGenerator[randomPosition];
                    lotteryGenerator[randomPosition] = temp;
                }           
            }     
        
            //Sorting the numbers in the array
            public void sortNumbers()
            {
               for(int i=0; i < lotteryGenerator.length-1; i++) 
                {
                   for(int j=0; j < lotteryGenerator.length-1-i; j++) 
                    {
                        if(lotteryGenerator[j] > lotteryGenerator[j+1]) 
                        {
                           int temp = lotteryGenerator[j];
                           lotteryGenerator[j] = lotteryGenerator[j+1];
                           lotteryGenerator[j+1] = temp;
                        }
                    }
                }        
                           
            }
        
           // Printing the numbers out
           public void showNums()
           {
              for (int i = 0; i < 6; i++)
              {
                   lottoNums += " " + lotteryGenerator[i] + " ";
              }
              lotteryText.setText(lottoNums);
           }
            
            public void actionPerformed(ActionEvent e)
            {
                createNumbers();
                shuffleNumbers();
                sortNumbers();
                showNums();
            }
    }

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

    Default Re: lottery GUI program

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

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: lottery GUI program

    Java Code:
                createNumbers();
                shuffleNumbers();
                sortNumbers();
                showNums();
    Maybe because you sort them every time after you shuffle them?

Similar Threads

  1. Lottery game
    By Festimd in forum New To Java
    Replies: 7
    Last Post: 06-06-2012, 10:08 PM
  2. Lottery Application
    By leoshiner in forum New To Java
    Replies: 2
    Last Post: 03-06-2012, 01:02 PM
  3. Lottery Java Program please help
    By grcarr77 in forum New To Java
    Replies: 1
    Last Post: 10-16-2011, 07:20 PM
  4. lottery
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 02-20-2011, 09:28 PM
  5. Lottery Application
    By notnumber6 in forum New To Java
    Replies: 3
    Last Post: 11-03-2007, 11:42 AM

Posting Permissions

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