I have to create a program that makes a lottery like game. My teacher wants me to use the constructor to initialize all the fields but I can't seem to get it to do that. Everytime I put the fields in the constructor initialized, I get compile errors. So right now, I end up callin my methods in the constructor. How can i initialize the random numbers and get it to compile?

/*
*@author Brock Shelton
*Date: April 12,2010
*Purpose: Write a lotter class that stimulates a lottery
*/

import java.util.Scanner; //imports Scanner class
import java.util.Random; //import Random class


public class Lottery
 {

        private int[] lucky = new int[5];
        private int win;
        private Random r1 = new Random(); 
        private Random r2 = new Random();
        private Random r3 = new Random();
		  private Random r4 = new Random();
		  private Random r5 = new Random();

        private int num1 = r1.nextInt(10);
        private int num2 = r2.nextInt(10);
        private int num3 = r3.nextInt(10);
		  private int num4 = r4.nextInt(10);
		  private int num5 = r5.nextInt(10);

        

        public Lottery()
		  {
		  		int win = 0;
            getNumbers();
            pick(lucky);
			   output();                      
        }
		  
		  public void output()
		  {
		  	final String RULE = "Lottery winnings: $10,000 for 5 matching numbers \n" +
									 "$100 for 3 matching numbers \n" +
									 "$10 for 1 matching number \n";
		   final String EMPTY = "";
			final String WIN_NUM = "The winning numbers are ";
			final String YOU_WIN = "You Won $";
		  	
		    System.out.println(RULE);
          System.out.println(EMPTY);
          System.out.println(WIN_NUM);
          System.out.println(num1);
          System.out.println(num2);
          System.out.println(num3);
			 System.out.println(num4);
			 System.out.println(num5);
          System.out.println(EMPTY);
          System.out.println(YOU_WIN + win);  
		  }	
       
        public void pick(int[]lucky)
		  {             
        	 if(lucky[0] == num1 && lucky[1] == num2 && lucky[2] == num3 && lucky[3] == num4 && lucky[4] == num5)
				{
         		win = 100000;
         		 return;
        	 }

                
                
       	for (int i=0;i<lucky.length;i++)
		 	 {                              
         	if( ((lucky[i] == num1)&&(lucky[i] == num2))||
          	     ((lucky[i] == num2)&&(lucky[i] == num3))||
           	    ((lucky[i] == num3)&&(lucky[i] == num2))||
					 ((lucky[i] == num4)&&(lucky[i] == num3))||
					 ((lucky[i] == num5) &&(lucky[i] == num4)))
					{
	             	  win = 100;
	                return;
          	   }
                        
            if (lucky[i] == num1 || lucky[i] == num2 || lucky[i] == num3 || lucky[i] == num4 || lucky[i] == num5)
				{
               win = 10;                    
            }
       	 }
                return;
                
			}
			
        public void getNumbers()
		  {
		  	int i;
		 	Scanner sc = new Scanner(System.in);  
			
		  	final String FIRST = "Enter the first number ";
			final String SECOND = "Enter the second number ";
			final String THIRD = "Enter the third number ";
			final String FOURTH = "Enter the fourth number ";
			final String FIFTH = "Enter the fifth number ";
			
      	  System.out.println(FIRST);             
       	   i = sc.nextInt();
        		 lucky[0] = i%10;
         
         	System.out.println(SECOND);           
          	i = sc.nextInt();
         	lucky[1] = i%10;
        
            System.out.println(THIRD);           
            i = sc.nextInt();
            lucky[2] = i%10;
				
		      System.out.println(FOURTH);           
            i = sc.nextInt();
            lucky[3] = i%10;
				
			   System.out.println(FIFTH);           
            i = sc.nextInt();
            lucky[4] = i%10;
        }
		  
		  
	 public static void main (String args[])
	 {
        Lottery lottery = new Lottery();    
    }
        
		  
}

Many teachers consider it a violation of academic integrity to post your code online, so you should be careful about putting your name on things and about posting exact code if that is the case with your teacher.

/*
*@author Brock Shelton
*Date: April 12,2010
*Purpose: Write a lotter class that stimulates a lottery
*/

import java.util.Scanner; //imports Scanner class
import java.util.Random; //import Random class


public class Lottery
 {

        private int[] lucky = new int[5];
        private int win;
        private Random r1; 
        private Random r2 = new Random();
        private Random r3 = new Random();
		  private Random r4 = new Random();
		  private Random r5 = new Random();

        private int num1 = r1.nextInt(10);
        private int num2 = r2.nextInt(10);
        private int num3 = r3.nextInt(10);
		  private int num4 = r4.nextInt(10);
		  private int num5 = r5.nextInt(10);

        

        public Lottery()
		  {
            r1 = new Random();

	win = 0;
            getNumbers();
            pick(lucky);
			   output();                      
        }
		  
}

Notice how I changed your 'int win' declaration; you made a common beginner mistake. If you declare a variable inside of a method, the variable no longer exists outside of that method (and cannot be referred to outside of that method). So if you wanted to refer to your 'win' variable that you declared as "private int win" you'd just say "win = ..." or "this.win = ..." inside of a method. If you declare a variable as a class variable (for example, your r1-r5 and num1-num5 variable are all class variables) it can be used inside of any method in the class, and every time you say MyClassName var = new MyClassName(), the variable "var" now has all of those members. So for example, after you say Lottery myLotto = new Lottery(), the 'myLotto' variable now has all of the r1-r5 variable and all of the num1-num5 variables.

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.