I have a lottery program nearly completed, thought i had the last of the problems sorted but it seems i did not!!
The program has a couple of issues.
1: Everytime i click a button it stores what i clicked, for example 10, and also creates a random number. Problem is what's happening is i click my first choice, it compares it to the first random number, i click choice 2, it compares it to random number 2, and so on...naturally this is a problem. I want it to take my 6 inputted numbers, then compare Against ALL the 6 random numbers it generated for me, not on a 1 on 1 basis..
The problem is in the logic in my "for" loop but i just can't seem to get it i've tried all avenues!!

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.util.Random;
import javax.swing.JOptionPane;

public class DisplayButtonMessage
{
        //display action command of jbutton
         int matches=0;
           int loop=0; 

    public DisplayButtonMessage()//Main constructor
    {
        //method for the everything that happens


        JFrame frame = new JFrame("Lottery App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridLayout(11, 4));//create gridlayput to hold buttons



        ActionListener al = new ActionListener() 
        {

            public void actionPerformed(ActionEvent event) 
            {

           int arr1[];
       arr1=new int[6];

           loop++;
           int number=0; 
           int counter;

 ////////////////////////////////////////////////////////////////////

                String num = ((JButton) event.getSource()).getActionCommand();
          JOptionPane.showConfirmDialog(null, "Are you sure you want? "+num);   



                  int newNum= Integer.parseInt(num);//Number is now an integer


                  for(counter=0; counter<1; counter++)
                  {
                  Random dice=new Random();
                  number =dice.nextInt(45); 
                  arr1[counter]=number;
                //Creates 6 random numbers for me
             System.out.println(arr1[counter]);

             if(newNum==arr1[counter])
             {
             matches++; 


             }



                  //If my original numbers equal any of the random ones
                  //It adds one to a matches counter



              if(loop==6)
              {


                  JOptionPane.showMessageDialog(null,"Now to the Main Draw for a JACKPOT of 5 million!!");



                JOptionPane.showMessageDialog(null, "Let's review your matches! ");    

                   if(matches==0)
                  {
         JOptionPane.showMessageDialog(null, "No matches !  ");            
                  }

                  if(matches==1)
                  {
         JOptionPane.showMessageDialog(null, "Just One number, Maybe next time!  ");            
                  }

                        if(matches==2)
                  {
         JOptionPane.showMessageDialog(null, "You Won a scratch Ticket ");            
                  }

                        if(matches==3)
                  {
         JOptionPane.showMessageDialog(null, "You Won 25 euros!");            
                  }


                if(matches==4)
                  {
         JOptionPane.showMessageDialog(null, "You Won 50,000 euros!");            
                  }        

                        if(matches==5)
                  {
         JOptionPane.showMessageDialog(null, "You Won 500,00 euros! ");            
                  }

                        if(matches==6)
                  {
         JOptionPane.showMessageDialog(null, "You Won The JACKPOT!! ");            
                  }

              }



               } 






                  }








        };     

 ///////////////////////////////////////////////////////////////////////////////








        for (int i = 0; i < 44; i++) 
        {
            JButton b = new JButton("No: " + String.valueOf((i + 1)));
            b.setBackground(Color.ORANGE);
            //Creates a button each iteration 
            //It labels each number the value of counter+1



            b.setActionCommand(String.valueOf((i + 1)));
            //This gets the value of the button, counter + 1 also

             b.addActionListener(al);//Adds action listener for above action Performed  



            panel.add(b);//Adds "b" to panel


        }















        frame.add(panel);//Adds the panel to frame

        frame.pack();
        frame.setVisible(true);//Sets the frame visible




    }



 }

Recommended Answers

All 2 Replies

I tried to maintain your structure.
This works. You have to make it continue if a game is over.

package GridBagLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Random;
import javax.swing.JOptionPane;
public class DisplayButtonMessage
{
//display action command of jbutton
int matches=0;
int loop = 0;
int arr1[] =new int[6];
int numbers[] = new int[6];
public DisplayButtonMessage()//Main constructor
{
//method for the everything that happens
JFrame frame = new JFrame("Lottery App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(11, 4));//create gridlayput to hold buttons
ActionListener al = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
loop++;
int number=0;
////////////////////////////////////////////////////////////////////
String num = ((JButton) event.getSource()).getActionCommand();
JOptionPane.showConfirmDialog(null, "Are you sure you want? "+num);
int newNum= Integer.parseInt(num);//Number is now an integer
numbers[loop-1] = newNum;
Random dice=new Random();
number =dice.nextInt(45);
arr1[loop-1]=number;

if(loop==6)
{
 for (int i = 0; i<6; i++){  
     for(int j =0; j<6; j++)
     {
      if(numbers[i] == arr1[j])  matches++;  
     }
 }

   for (int i = 0; i<6; i++){  
  System.out.println("number [" + i + "] = " + numbers[i] +" arr1 [" +i + " ] =" + arr1[i]);  
   }

JOptionPane.showMessageDialog(null,"Now to the Main Draw for a JACKPOT of 5 million!!");
JOptionPane.showMessageDialog(null, "Let's review your matches! ");
if(matches==0)
{
JOptionPane.showMessageDialog(null, "No matches ! ");
}
if(matches==1)
{
JOptionPane.showMessageDialog(null, "Just One number, Maybe next time! ");
}
if(matches==2)
{
JOptionPane.showMessageDialog(null, "You Won a scratch Ticket ");
}
if(matches==3)
{
JOptionPane.showMessageDialog(null, "You Won 25 euros!");
}
if(matches==4)
{
JOptionPane.showMessageDialog(null, "You Won 50,000 euros!");
}
if(matches==5)
{
JOptionPane.showMessageDialog(null, "You Won 500,00 euros! ");
}
if(matches==6)
{
JOptionPane.showMessageDialog(null, "You Won The JACKPOT!! ");
}
}
}
}; 
 for (int i = 0; i < 44; i++)
{
JButton b = new JButton("No: " + String.valueOf((i + 1)));
b.setBackground(Color.ORANGE);
//Creates a button each iteration
//It labels each number the value of counter+1
b.setActionCommand(String.valueOf((i + 1)));
//This gets the value of the button, counter + 1 also
b.addActionListener(al);//Adds action listener for above action Performed
panel.add(b);//Adds "b" to panel
}

frame.add(panel);//Adds the panel to frame
frame.pack();
frame.setVisible(true);//Sets the frame visible
}
 public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DisplayButtonMessage dbm = new DisplayButtonMessage();
            }
        });
    }

}

Also you have to correct the use of JOptionPane.showConfirmDialog() since it does nothing if you select no or cancel.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.