# Lottery help, looping problem

• 10-31-2010, 05:36 PM
Lottery help, looping problem
I'm new to these forums :( So I don't know the format that I should be posting questions in so I'm sorry if I post incorrectly.

Anyway, I'm making a lottery program for class similar to lotto 6/49 in Ontario, Canada. So far I have made it so that you enter your numbers, you pick how many numbers you want it to match, and then it picks non-repeating numbers from 1-49 and compares them.

Now it doesn't really compare them the way I want it to. What's happening is that I will pick the numbers and what will happen is it will keep on looping and spit out sets of 6 random numbers and compare them, the problem is, is that the computer is comparing ALL the random numbers, even the ones from the previous sets of random numbers. For example I pick: 1,2,3,4,5,6 and want to match 3 numbers, so I press go. Now I can see in the system output that the draws are:
23
35
45
32
24
31

40
29
37
6
13
34

1
33
4
25
42
18

As you can see it is matching numbers from previous draws making all my draws take only a maximum of 5 sets to match which is unrealistic.

Here is my code for the loop:

Code:

```  {     if(e.getSource()==button1)       for(int g=0;g<6;g++)     {       entries[g]=Integer.parseInt(t[g].getText());     }     numbertomatch=Integer.parseInt(matchnumber.getText());     {       while(numbertomatch>matches)       {         {           for(int m=0;m<6;m++)           {             do             {               x=(int)(Math.random()*49)+1;             }while(checklist[x]==1);                         d[m].setText(""+x); //placing RN into a Label             draws[m]=x; //storing RN to appropriate             checklist[x]=1;             tries++;             System.out.println(x);                                     for(int q=0;q<6;q++)             {               for(int u=0;u<6;u++)               {                 if(draws[u]==entries[q])                 {                   matches++;                 }               }             }             draws[m]=0;             plays=new JLabel ("It took "+ tries +" plays to match.");           }         }       }     }   }```

I also wanted it to count how many times it took to match the numbers so I used the variable "tries" to count that, but it isn't counting. I think the problems are related. Any help is welcome, and here is my entire code if you'd like to try it out.

Code:

```import javax.swing.*; import java.awt.*; import java.awt.event.*; public class nonrigged649 extends JFrame implements ActionListener{   int x=0, matches=0, numbertomatch=0, tries=0;   int []checklist;   int []entries;   int []draws;   JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;   JButton button1;   JTextField []t;   JTextField matchnumber;   JLabel title, tomatch, plays;   JLabel []d;     public static void main(String[ ] args)   {     new nonrigged649 ();   }      public nonrigged649 (){     panel1=new JPanel();     panel1.setLayout(new BorderLayout());     panel2=new JPanel();     panel2.setLayout(new BorderLayout());     panel3=new JPanel();     panel3.setLayout(new GridLayout(1,6));     panel4=new JPanel();     panel4.setLayout(new GridLayout (1,6));     panel5=new JPanel();     panel5.setLayout(new BorderLayout());     panel6=new JPanel();     panel6.setLayout(new GridLayout (2,1));     panel7=new JPanel();     panel7.setLayout(new GridLayout (2,1));     panel8=new JPanel();     panel8.setLayout(new BorderLayout());         matchnumber=new JTextField();     plays=new JLabel ("It took "+ matches +" plays to match.");     title=new JLabel("Lotto 6/49");     tomatch=new JLabel("How many numbers to match?");         this.add(panel1);     panel1.add(panel2, BorderLayout. NORTH);     panel1.add(panel7, BorderLayout. SOUTH);     panel1.add(panel5, BorderLayout. CENTER);     panel5.add(panel4, BorderLayout. NORTH);     panel2.add(title, BorderLayout. CENTER);     panel5.add(panel6, BorderLayout. CENTER);         panel7.add(panel3);     panel7.add(panel8);         panel8.add(plays);     panel6.add(tomatch);     panel6.add(matchnumber);     button1=new JButton ("GO");     button1.addActionListener(this);     panel5.add(button1, BorderLayout. SOUTH);         draws= new int[6];     entries= new int[6];     d=new JLabel[6];     for(int a=0;a<6;a++)     {       d[a]=new JLabel (" ");       panel3.add(d[a]);                 }     title=new JLabel("Lotto 6/49");         t=new JTextField[6];     for(int b=0;b<6;b++)     {       t[b]=new JTextField ("");       panel4.add(t[b]);             entries[b]=0;     }             checklist=new int[50];         this.setSize(200,200);     this.setVisible(true);   }       public void actionPerformed(ActionEvent e)   {     if(e.getSource()==button1)       for(int g=0;g<6;g++)     {       entries[g]=Integer.parseInt(t[g].getText());     }     numbertomatch=Integer.parseInt(matchnumber.getText());     {       while(numbertomatch>matches)       {         {           for(int m=0;m<6;m++)           {             do             {               x=(int)(Math.random()*49)+1;             }while(checklist[x]==1);                         d[m].setText(""+x); //placing RN into a Label             draws[m]=x; //storing RN to appropriate             checklist[x]=1;             tries++;             System.out.println(x);                                     for(int q=0;q<6;q++)             {               for(int u=0;u<6;u++)               {                 if(draws[u]==entries[q])                 {                   matches++;                 }               }             }             draws[m]=0;             plays=new JLabel ("It took "+ tries +" plays to match.");           }         }       }     }   } }```