Results 1 to 3 of 3
  1. #1
    hadoken5 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default 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:

    Java 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.


    Java 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.");
              }
            }
          }
        }
      }
    }
    Moderator Edit: code tags added
    Last edited by Fubarable; 10-31-2010 at 05:46 PM. Reason: Moderator Edit: code tags added

  2. #2
    hadoken5 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Come on, help please? :(

  3. #3
    hadoken5 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Ok I figured out that I need to set matches to 0 before every loop to solve my problem. My only problem now is that I don't know how to reset checklist without breaking its function, which is to prevent the random number from repeating. I don't know if this forum is dead but anyone out the help me please :(

Similar Threads

  1. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 06:57 AM
  2. Problem With Looping [JAVA]
    By jude113 in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 02:00 PM
  3. problem with <logic:iterate> tag looping
    By tsaswathy in forum Web Frameworks
    Replies: 0
    Last Post: 09-27-2008, 01:13 PM
  4. Looping problem
    By Tanilo in forum New To Java
    Replies: 1
    Last Post: 08-01-2008, 07:34 AM
  5. Lottery Application
    By notnumber6 in forum New To Java
    Replies: 3
    Last Post: 11-03-2007, 12:42 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
  •