So the way I understand your question is as follows: You want a program that generates a set of 6 random numbers each number within the range 1 to 50 - this represents the correct lottery numbers. You then want to have the program generate random sets of 6 numbers each of which is within the range 1 to 50 until one of these sets matches the lottery numbers. You also want the program to keep track of how many guesses were made before a win was achieved - I am assuming that this means lottery numbers are chosen once and guesses are made continually until a match occurs.
Note: I do not see logic in your code sample for tracking the number of guesses, but I did implement this feature in my code below.
It is not clear whether the order of the lottery numbers is important. I wrote the code below such that the order is not important, meaning that as long as the guess contains all 6 of the lottery numbers it is considered a winner. If you want order to be important you should remove the call to Arrays.sort(...)
Here is a solution that uses arrays since you specifically said you wanted to use arrays:
import java.util.Arrays;
import java.util.Random;
public class LotteryMain {
public static void main(String[] args) {
int[] correctNumbers = createSortedArrayOfRandomInts(6);
System.out.println("Lottery numbers: "+Arrays.toString(correctNumbers));
int tries = 0;
int[] guessNumbers = null;
do{
guessNumbers = createSortedArrayOfRandomInts(6);
System.out.println("Guessed numbers: "+Arrays.toString(guessNumbers));
tries++;
}while(!Arrays.equals(guessNumbers, correctNumbers));
System.out.println("Lottery numbers: "+Arrays.toString(correctNumbers));
System.out.println("WINNER! Number of tries: "+tries);
}
public static int[] createSortedArrayOfRandomInts(int sizeOfArray){
int[] arrayOfRandomNumbers = new int[sizeOfArray];
Random randomGen = new Random();
for (int i = 0; i < sizeOfArray; i++) {
int randomNum = -1;
do{
randomNum = randomGen.nextInt(50)+1; // Random number created here.
}while(contains(arrayOfRandomNumbers, randomNum));
arrayOfRandomNumbers[i] = randomNum;
}
Arrays.sort(arrayOfRandomNumbers);
return arrayOfRandomNumbers;
}
public static boolean contains(int[] array, int searchFor){
for(int i = 0; i < array.length; i++){
if(array[i] == searchFor)
return true;
}
return false;
}
}
Here is sample output (shortened, obviously):
...
Guessed numbers: [5, 21, 27, 36, 40, 47]
Guessed numbers: [3, 14, 15, 28, 36, 42]
Guessed numbers: [24, 27, 31, 32, 44, 49]
Guessed numbers: [19, 21, 25, 39, 42, 46]
Lottery numbers: [19, 21, 25, 39, 42, 46]
WINNER! Number of tries: 2499043
Here is a solution using classes from the Java Collections Framework because every Java programmer should be familiar with how to use these classes:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class LotteryWithCollections {
public static void main(String[] args) {
List<Integer> correctNumbers = createSortedListOfRandomInts(6);
System.out.println("Lottery numbers: " + correctNumbers);
int tries = 0;
List<Integer> guessNumbers = null;
do {
guessNumbers = createSortedListOfRandomInts(6);
System.out.println("Guessed numbers: " + guessNumbers);
tries++;
} while (!guessNumbers.equals(correctNumbers));
System.out.println("Lottery numbers: " + correctNumbers);
System.out.println("WINNER! Number of tries: " + tries);
}
public static List<Integer> createSortedListOfRandomInts(int sizeOfList) {
List<Integer> listOfRandomNumbers = new ArrayList<Integer>();
Random randomGen = new Random();
for (int i = 0; i < sizeOfList; i++) {
int randomNum = randomGen.nextInt(50) + 1; // Random number created
// here.
while (listOfRandomNumbers.contains(randomNum)) {
randomNum = randomGen.nextInt(50) + 1;
}
listOfRandomNumbers.add(randomNum);
}
Collections.sort(listOfRandomNumbers);
return listOfRandomNumbers;
}
}
Something else to consider - right now the code allows for duplicate guesses, it might be interesting to keep track of previous guesses and disallow repeats of the same guess.