# Problem on what to do next with Lottery game in Java

Page 1 of 1

## 10 Replies - 652 Views - Last Post: 11 August 2012 - 03:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288433&amp;s=93c2ba59b8dc31f851e3dd09649fb9ad&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 wilddrummer

Reputation: 0
• 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 ();
}
}

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

• D.I.C Lover

Reputation: 994
• 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

```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);

```

### #3 g00se

• D.I.C Lover

Reputation: 2110
• 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?

### #4 SPorter

Reputation: 11
• 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

### #5 wilddrummer

Reputation: 0
• 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);
```

How would the isSelected method look after this?

### #6 g00se

• D.I.C Lover

Reputation: 2110
• 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

### #7 CasiOo

• D.I.C Lover

Reputation: 994
• 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.

### #8 wilddrummer

Reputation: 0
• 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));

b1.setText("X");
}

public void actionPerformed(ActionEvent e)
{

}

public static void main(String[] args)
{
JClickTheX mouse = new JClickTheX();
mouse.setVisible(true);
}
}

```

### #9 macosxnerd101

• Self-Trained Economist

Reputation: 9029
• 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

### #10 wilddrummer

Reputation: 0
• 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!

### #11 g00se

• D.I.C Lover

Reputation: 2110
• 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