Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Lottery Game Problem

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Lottery Game Problem

    we need to create a Lottery game where the user must select 6 numbers, and the program will generate 6 random numbers. and the program will find a match and will display appropriate text depending on how many matches were made.

    heres my problem i cannot assign numbers properly to the checked checkboxes(if you run the program you can see what i mean)


    /**
     * @(#)lottoDraw.java
     *
     *
     * @author 
     * @version 1.00 2012/12/1
     */
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class lottoDraw extends JFrame implements ActionListener, ItemListener
    {
    	JCheckBox b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30;
    	JLabel lottoName,result,pickedNumbers,reward;
    	JButton draw,exit;
    	JPanel p1,p2,p3,p4,p5,p6,p7,p8;
     
    	JFrame myFrame = new JFrame("Lotto");
     
    	GridLayout layout = new GridLayout(1,1);
     
    	int resultNum1 = 0 + (int)(Math.random() * ((29 - 0) + 1));
    	int resultNum2 = 0 + (int)(Math.random() * ((29 - 0) + 1));
    	int resultNum3 = 0 + (int)(Math.random() * ((29 - 0) + 1));
    	int resultNum4 = 0 + (int)(Math.random() * ((29 - 0) + 1));
    	int resultNum5 = 0 + (int)(Math.random() * ((29 - 0) + 1));
    	int resultNum6 = 0 + (int)(Math.random() * ((29 - 0) + 1));
     
    	int check;
    	int num [] = {0,0,0,0,0,0};
    	int turn [] = {0,0,0,0,0,0};
     
        public lottoDraw() 
        {
        	myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	myFrame.setSize(700, 500);
        	myFrame.setVisible(true);
        	myFrame.setLayout(new GridLayout(8,1));
     
        	p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        	p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        	p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        	p4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        	p5 = new JPanel(layout);
        	p6 = new JPanel(layout);
        	p7 = new JPanel(layout);
        	p8 = new JPanel(layout);
     
        	b0 = new JCheckBox("0");
        	b1 = new JCheckBox("1");
        	b2 = new JCheckBox("2");
        	b3 = new JCheckBox("3");
        	b4 = new JCheckBox("4");
        	b5 = new JCheckBox("5");
        	b6 = new JCheckBox("6");
        	b7 = new JCheckBox("7");
        	b8 = new JCheckBox("8");
        	b9 = new JCheckBox("9");
        	b10 = new JCheckBox("10");
        	b11 = new JCheckBox("11");
        	b12 = new JCheckBox("12");
        	b13 = new JCheckBox("13");
        	b14 = new JCheckBox("14");
        	b15 = new JCheckBox("15");
        	b16 = new JCheckBox("16");
        	b17 = new JCheckBox("17");
        	b18 = new JCheckBox("18");
        	b19 = new JCheckBox("19");
        	b20 = new JCheckBox("20");
        	b21 = new JCheckBox("21");
        	b22 = new JCheckBox("22");
        	b23 = new JCheckBox("23");
        	b24 = new JCheckBox("24");
        	b25 = new JCheckBox("25");
        	b26 = new JCheckBox("26");
        	b27 = new JCheckBox("27");
        	b28 = new JCheckBox("28");
        	b29 = new JCheckBox("29");
        	b30 = new JCheckBox("30");
     
        	lottoName = new JLabel("WIN WIN WIN");
        	result = new JLabel();
        	pickedNumbers = new JLabel();
        	reward = new JLabel();
     
        	draw = new JButton("Draw");
        	exit = new JButton("Exit");
     
        	myFrame.add(p1);
        	myFrame.add(p2);
        	myFrame.add(p3);
        	myFrame.add(p4);
        	myFrame.add(p5);
        	myFrame.add(p6);
        	myFrame.add(p7);
        	myFrame.add(p8);
     
        	p1.add(lottoName);
        	p2.add(pickedNumbers);
        	p3.add(result);
        	p4.add(reward);
     
        	p5.add(b0);
        	p5.add(b1);
        	p5.add(b2);
        	p5.add(b3);
        	p5.add(b4);
        	p5.add(b5);
        	p5.add(b6);
        	p5.add(b7);
        	p5.add(b8);
        	p5.add(b9);
        	p5.add(b10);
        	p6.add(b11);
        	p6.add(b12);
        	p6.add(b13);
        	p6.add(b14);
        	p6.add(b15);
        	p6.add(b16);
        	p6.add(b17);
        	p6.add(b18);
        	p6.add(b19);
        	p6.add(b20);
        	p6.add(b21);
        	p7.add(b22);
        	p7.add(b23);
        	p7.add(b24);
        	p7.add(b24);
        	p7.add(b25);
        	p7.add(b26);
        	p7.add(b27);
        	p7.add(b28);
        	p7.add(b29);
        	p7.add(b30);
     
        	p8.add(draw);
        	p8.add(exit);
     
        	b0.addItemListener(this);
        	b1.addItemListener(this);
        	b2.addItemListener(this);
        	b3.addItemListener(this);
        	b4.addItemListener(this);
        	b5.addItemListener(this);
        	b6.addItemListener(this);
        	b7.addItemListener(this);
        	b8.addItemListener(this);
        	b9.addItemListener(this);
        	b10.addItemListener(this);
        	b11.addItemListener(this);
        	b12.addItemListener(this);
        	b13.addItemListener(this);
        	b14.addItemListener(this);
        	b15.addItemListener(this);
        	b16.addItemListener(this);
        	b17.addItemListener(this);
        	b18.addItemListener(this);
        	b19.addItemListener(this);
        	b20.addItemListener(this);
        	b21.addItemListener(this);
        	b22.addItemListener(this);
        	b23.addItemListener(this);
        	b24.addItemListener(this);
        	b25.addItemListener(this);
        	b26.addItemListener(this);
        	b27.addItemListener(this);
        	b28.addItemListener(this);
        	b29.addItemListener(this);
        	b30.addItemListener(this);
     
        	draw.addActionListener(this);	
     
        }
     
        public void itemStateChanged(ItemEvent e)
        {
        	Object source = e.getSource();
        	Object select = e.getStateChange();
     
        	for(int x=0;x<num.length;x++)
        	{
        		if(source == b0)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 0;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b1)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 1;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b2)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 2;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b3)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 3;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b4)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 4;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b5)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 5;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b6)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 6;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b7)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 7;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b8)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 8;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b9)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 9;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b10)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 10;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b11)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 11;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b12)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 12;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b13)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 13;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b14)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 14;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b15)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] =15;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b16)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 16;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b17)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 17;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b18)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 18;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b19)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 19;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b20)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 20;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b21)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 21;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b22)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 22;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b23)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 23;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b24)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 24;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b25)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 25;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b26)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 26;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}		
        		}
     
        		if(source == b27)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 27;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b28)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 28;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b29)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 29;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        		if(source == b30)
        		{
        			if(select == ItemEvent.SELECTED)
        			{
        				num[x] = 30;
        			    check++;
        			    break;
        			}
     
        			else
        			{
        				num[x] = 0;
        				check--;
        				break;
        			}
     
        		}
     
        	}
     
        }
     
        public void actionPerformed(ActionEvent a)
        {
        	Object source = a.getSource();
     
        	pickedNumbers.setText(num[0] + " " + num[1] + " " + num[2] + " " + num[3] + " " + num[4] + " " + num[5]);
        	result.setText(resultNum1 + " " + resultNum2 + " " + resultNum3 + " " + resultNum4 + " " + resultNum5 + " " + resultNum6);
        }
     
        public static void main(String[]args)
        {
        	new lottoDraw();
        }
     
     
    }

    help would be appreciated. thanks in advance.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Lottery Game Problem

    No one here has time to go through the whole piece of code. Ask your questions Precisely if you need help. SSCCE
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Lottery Game Problem

    ok then. when the user clicks on the checkbox 5 the program must assign the variable num1 to 5 and when the checkbox 10 is clicked num2 should be assigned 10 and so on, but here is my i cant make it do that. instead the program only assigns the num1 with the last checkboxed clicked and shows the other variable num as 0.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Lottery Game Problem

    You will have to do one of two things:
    1. Create click listener seperate for each check box, and in each checkbox, put the variable you want to assign value with.
    2. In your single click listener, write conditions to check which checkbox is selected and assign the related variables, value with.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  5. #5
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Lottery Game Problem

    i have already done that. but the last clicked checkbox is only one showing its right assigned value. ex when i click the checkboxes with the label 4 & 5(simultaneously) the program only shows 5 and does not show 4.

Similar Threads

  1. Simple game problem
    By frozen java in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 30th, 2011, 08:14 PM
  2. Game problem
    By Yo Cas Cas in forum What's Wrong With My Code?
    Replies: 13
    Last Post: August 25th, 2011, 07:52 PM
  3. Card Game Problem.
    By d'fitz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 07:30 AM
  4. Card Game Problem....Need Help
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2011, 07:30 AM
  5. [SOLVED] minesweeper game creation problem
    By kaylors in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 27th, 2009, 04:06 PM