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();
}
}