10 Replies - 652 Views - Last Post: 11 August 2012 - 03:40 AM Rate Topic: -----

#1 wilddrummer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Problem on what to do next with Lottery game in Java

Posted 09 August 2012 - 01:28 PM

Creat a game using check boxes. For this game, generate six random numbers, each between 0 and 30 inclusive. Allow the user to choose six check boxes to play the game. (Do not allow the user to choose more than six boxes.) After the player has choosen six numbers, display the randomly selected numbers, the player's numbers and the amount of money the user has won as follows:
Three matches 100, four matches 10,000, five matches 50,000, six matches 1,000,000, and zero, one, or two matches 0.

I have searched online and found nothing that was useful or made sense. My code is below, but my question is how do I limit the selection to 6 check boxes and then how would I display the results?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JLottery extends JFrame{
    
    FlowLayout flow = new FlowLayout();
    JCheckBox[] boxes = new JCheckBox [30];
    int i;
    
    int num1 = (int)(Math.random()*30+1);
    int num2 = (int)(Math.random()*30+1);
    int num3 = (int)(Math.random()*30+1);
    int num4 = (int)(Math.random()*30+1);
    int num5 = (int)(Math.random()*30+1);
    int num6 = (int)(Math.random()*30+1);
        
  
    public JLottery()
    {
        super ("Lottery");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout (new FlowLayout());
        
        for ( i=0 ; i < 30; i++)
        {
            boxes[i] = new JCheckBox ();
            add(boxes[i]);
        }
    }
  
    
public static void main(String[] args)
        {
            JLottery myFrame = new JLottery();
            myFrame.setSize(750,750);
            myFrame.setVisible(true);
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Problem on what to do next with Lottery game in Java

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,203
  • Joined: 05-April 11

Re: Problem on what to do next with Lottery game in Java

Posted 09 August 2012 - 01:49 PM

Make a variable that holds how many checkboxes have been checked. You will have to add a listener to your checkboxes so you can adjust the variable when a checkbox is checked/unchecked
The isSelected() method may become useful :)

How about an array here
10	    int num1 = (int)(Math.random()*30+1);
11	    int num2 = (int)(Math.random()*30+1);
12	    int num3 = (int)(Math.random()*30+1);
13	    int num4 = (int)(Math.random()*30+1);
14	    int num5 = (int)(Math.random()*30+1);
15	    int num6 = (int)(Math.random()*30+1);


Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: Problem on what to do next with Lottery game in Java

Posted 09 August 2012 - 01:51 PM

Is it legitimate to have repeated numbers?
Was This Post Helpful? 0
  • +
  • -

#4 SPorter  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 31
  • Joined: 29-June 12

Re: Problem on what to do next with Lottery game in Java

Posted 09 August 2012 - 02:31 PM

g00se brings up a very good point. There is a code snippet in the code snippet section showing how you can generate lotto numbers which do not repeat. http://www.dreaminco...snippet2992.htm
Was This Post Helpful? 0
  • +
  • -

#5 wilddrummer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: Problem on what to do next with Lottery game in Java

Posted 09 August 2012 - 02:57 PM

ahh, I'm new with java and my mind is fried with Java info right now. I'm not sure if I'm even on the right track with the code I have. :/ Would the listeners look like this...?

box1.addItemListener(this);
        box2.addItemListener(this);
        box3.addItemListener(this);
        box4.addItemListener(this);
        box5.addItemListener(this);
        box6.addItemListener(this);
        box7.addItemListener(this);
        box8.addItemListener(this);
        box9.addItemListener(this);
        box10.addItemListener(this);
        box11.addItemListener(this);
        box12.addItemListener(this);
        box13.addItemListener(this);
        box14.addItemListener(this);
        box15.addItemListener(this);
        box16.addItemListener(this);
        box17.addItemListener(this);
        box18.addItemListener(this);
        box19.addItemListener(this);
        box20.addItemListener(this);
        box21.addItemListener(this);
        box22.addItemListener(this);
        box23.addItemListener(this);
        box24.addItemListener(this);
        box25.addItemListener(this);
        box26.addItemListener(this);
        box27.addItemListener(this);
        box28.addItemListener(this);
        box29.addItemListener(this);
        box30.addItemListener(this);


How would the isSelected method look after this?
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: Problem on what to do next with Lottery game in Java

Posted 09 August 2012 - 03:06 PM

As CasiOo said, you should be using an array (JCheckBox[])

See http://technojeeves....-random-numbers for random

Oops, i see you are using an array. But you're not actually using it. It should also be declared

public static final int RANDOM_RANGE_LENGTH = 31;
...
JCheckBox[] boxes = new JCheckBox [RANDOM_RANGE_LENGTH];


This post has been edited by g00se: 09 August 2012 - 03:12 PM
Reason for edit:: random

Was This Post Helpful? 1
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,203
  • Joined: 05-April 11

Re: Problem on what to do next with Lottery game in Java

Posted 09 August 2012 - 03:11 PM

I don't get it. You have made an array of JCheckBox'es but still you keep each of them box1, box2 ... boxN

When the checkbox is checked/unchecked it will fire an event, and you will then have to check if it is checked or not.
Was This Post Helpful? 1
  • +
  • -

#8 wilddrummer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: Problem on what to do next with Lottery game in Java

Posted 10 August 2012 - 03:42 PM

Thanks for everyone's help in advance.

I have placed an "X" on the first button and what I need it to do is this...
Once the user has clicked the "X" have the said "X" disappear and then reappear on a different button. This needs to be done 10 times, so I'm guessing some sort of loop.

I know I'm doing this the long way with out using an array or list but for whatever reason I was having trouble with that so I decided just to type out all 48 buttons. :/

This is my code so far
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JClickTheX extends JFrame implements ActionListener
{
	JButton b1 = new JButton();
	JButton b2 = new JButton();
	JButton b3 = new JButton();
	JButton b4 = new JButton();
	JButton b5 = new JButton();
	JButton b6 = new JButton();
	JButton b7 = new JButton();
	JButton b8 = new JButton();
	JButton b9 = new JButton();
	JButton b10 = new JButton();
	JButton b11 = new JButton();
	JButton b12 = new JButton();
	JButton b13 = new JButton();
	JButton b14 = new JButton();
	JButton b15 = new JButton();
	JButton b16 = new JButton();
	JButton b17 = new JButton();
	JButton b18 = new JButton();
	JButton b19 = new JButton();
	JButton b20 = new JButton();
	JButton b21 = new JButton();
	JButton b22 = new JButton();
	JButton b23 = new JButton();
	JButton b24 = new JButton();
	JButton b25 = new JButton();
	JButton b26 = new JButton();
	JButton b27 = new JButton();
	JButton b28 = new JButton();
	JButton b29 = new JButton();
	JButton b30 = new JButton();
	JButton b31 = new JButton();
	JButton b32 = new JButton();
	JButton b33 = new JButton();
	JButton b34 = new JButton();
	JButton b35 = new JButton();
	JButton b36 = new JButton();
	JButton b37 = new JButton();
	JButton b38 = new JButton();
	JButton b39 = new JButton();
	JButton b40 = new JButton();
	JButton b41 = new JButton();
	JButton b42 = new JButton();
	JButton b43 = new JButton();
	JButton b44 = new JButton();
	JButton b45 = new JButton();
	JButton b46 = new JButton();
	JButton b47 = new JButton();
	JButton b48 = new JButton();
	
	public JClickTheX()
	{
		setTitle("Click the X!");
		setSize(750, 750);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new GridLayout(8, 6));
		add(buttonPane);
		
		buttonPane.add(b1);
		buttonPane.add(b2);
		buttonPane.add(b3);
		buttonPane.add(b4);
		buttonPane.add(b5);
		buttonPane.add(b6);
		buttonPane.add(b7);
		buttonPane.add(b8);
		buttonPane.add(b9);
		buttonPane.add(b10);
		buttonPane.add(b11);
		buttonPane.add(b12);
		buttonPane.add(b13);
		buttonPane.add(b14);
		buttonPane.add(b15);
		buttonPane.add(b16);
		buttonPane.add(b17);
		buttonPane.add(b18);
		buttonPane.add(b19);
		buttonPane.add(b20);
		buttonPane.add(b21);
		buttonPane.add(b22);
		buttonPane.add(b23);
		buttonPane.add(b24);
		buttonPane.add(b25);
		buttonPane.add(b26);
		buttonPane.add(b27);
		buttonPane.add(b28);
		buttonPane.add(b29);
		buttonPane.add(b30);
		buttonPane.add(b31);
		buttonPane.add(b32);
		buttonPane.add(b33);
		buttonPane.add(b34);
		buttonPane.add(b35);
		buttonPane.add(b36);
		buttonPane.add(b37);
		buttonPane.add(b38);
		buttonPane.add(b39);
		buttonPane.add(b40);
		buttonPane.add(b41);
		buttonPane.add(b42);
		buttonPane.add(b43);
		buttonPane.add(b44);
		buttonPane.add(b45);
		buttonPane.add(b46);
		buttonPane.add(b47);
		buttonPane.add(b48);
		
		b1.addActionListener(this);
      b2.addActionListener(this);
      b3.addActionListener(this);
      b4.addActionListener(this);
      b5.addActionListener(this);
      b6.addActionListener(this);
      b7.addActionListener(this);
      b8.addActionListener(this);
      b9.addActionListener(this);
      b10.addActionListener(this);
      b11.addActionListener(this);
      b12.addActionListener(this);
      b13.addActionListener(this);
      b14.addActionListener(this);
      b15.addActionListener(this);
      b16.addActionListener(this);
      b17.addActionListener(this);
      b18.addActionListener(this);
      b19.addActionListener(this);
      b20.addActionListener(this);
      b21.addActionListener(this);
      b22.addActionListener(this);
      b23.addActionListener(this);
      b24.addActionListener(this);
      b25.addActionListener(this);
      b26.addActionListener(this);
      b27.addActionListener(this);
      b28.addActionListener(this);
      b29.addActionListener(this);
      b30.addActionListener(this);
		b31.addActionListener(this);
		b32.addActionListener(this);
		b33.addActionListener(this);
		b34.addActionListener(this);
		b35.addActionListener(this);
		b36.addActionListener(this);
		b37.addActionListener(this);
		b38.addActionListener(this);
		b39.addActionListener(this);
		b40.addActionListener(this);
		b41.addActionListener(this);
		b42.addActionListener(this);
		b43.addActionListener(this);
		b44.addActionListener(this);
		b45.addActionListener(this);
		b46.addActionListener(this);
		b47.addActionListener(this);
		b48.addActionListener(this);
		
		b1.setText("X");
	}
	
	public void actionPerformed(ActionEvent e)
	{
		
		
	}
		
			
			
	public static void main(String[] args)
	{
		JClickTheX mouse = new JClickTheX();
		mouse.setVisible(true);
	}
}


Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,490
  • Joined: 27-December 08

Re: Problem on what to do next with Lottery game in Java

Posted 10 August 2012 - 04:00 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#10 wilddrummer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: Problem on what to do next with Lottery game in Java

Posted 10 August 2012 - 04:03 PM

Its a different program with similar looks, sorry for any confusion!
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: Problem on what to do next with Lottery game in Java

Posted 11 August 2012 - 03:40 AM

Quote

I know I'm doing this the long way with out using an array or list but for whatever reason I was having trouble with that

Then you should ask, as that code's a complete PITA

As for your other question, if i've got you right, i'd add all button references to a Set<JButton>. In your ActionListener, remove the button clicked from that Set and choose another button at random from the remainder. Set its text to "X"

This post has been edited by g00se: 11 August 2012 - 03:43 AM
Reason for edit:: random

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1