I changed my checking to timesPlayed, and fixed some of the code to calculate the number times played. The loop runs 5,791 times before giving me a stackoverflow error. I really just want to know if there is a way to make it run until it reaches (numMatched == 5), or if that is impossible. Here's the new code if that helps. Thank you for all your help so far I really appreciate it.
New code:
import java.util.*;
public class LotteryArrays
{
public static void IsItAWinner(int[] lottoTicket, int[] lotteryNums, int timesPlayed) //Evaluates user and lottery numbers
{
int numMatched = 0;
for(int f=0;f<5;f++) //Finds the matching numbers
{
if(lottoTicket[f] == lotteryNums[f])
++numMatched;
}
System.out.println("User Numbers: "); //Displays results
for(int i=0;i<5;i++)
System.out.print(lottoTicket[i]);
System.out.println();
System.out.println("Lottery Numbers: ");
for(int i=0;i<5;i++)
System.out.print(lotteryNums[i]);
System.out.println();
System.out.println("Numbers Correct: " + numMatched);
if(numMatched == 5)
{
System.out.println("You are the GRAND PRIZE WINNER!");
System.out.println("You played " + timesPlayed + " to win the lottery");
}
if(timesPlayed != -1)
{
timesPlayed++;
System.out.println("You played " + timesPlayed + " times");
readData(lotteryNums, timesPlayed);
}
}
public static void readData(int[]lotteryNums, int timesPlayed) //Gets the users' pick for the lottery
{
int[] lottoTicket = new int[5];
for(int i=0;i<lottoTicket.length;i++)
lottoTicket[i] = (int)(Math.random()*10);
IsItAWinner(lottoTicket, lotteryNums, timesPlayed); //calls method to evaluate the lottery numbers and user numbers
}
public static void main(
String[] args)
{
int timesPlayed = 0;
int[] lotteryNums = new int[5];
for(int j=0;j<lotteryNums.length;j++)
lotteryNums[j] = (int)(Math.random()*10); //Generates lottery numbers
System.out.println("Choose your five lottery numbers:");
readData(lotteryNums, timesPlayed); //calls methods to pick numbers and analyze arrays
System.out.println("Thanks for playing!");
}
}