i wanted to create my first lottery_game. User has to type in 6 numbers, they are saved in array. In another array some random numbers are generated.
Now i want to compare the content. I want to know if my number of the first array i content of the second array. If yes the variable counter gets + 1. Then i want to know the counter.
It works until i wanted to know the counter. Then i get this error:
Please put in 6 numbers
5
8
9
7
4
5
Your numbers are: 5 8 9 7 4 5
The lottery numbers today are: 25 24 18 15 6 27 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Lotto_zwei.main(Lotto_zwei.java:48)
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Please put in 6 numbers"); //Start
Scanner input = new Scanner(System.in); //Scanner
int[] numbers = new int[6]; //Array of numbers
int counter = 0;
int[] lottery_numbers = new int[6];
int randomNum;
/* Loop_1 */
for(int i=0;i<numbers.length;i++) {
numbers[i] = input.nextInt();
}
/* Loop_2*/
System.out.print("Your numbers are: ");
for(int y : numbers) {
System.out.print(y + " ");
}
System.out.println(" ");
/* Loop_3 */
for(int i=0;i<lottery_numbers.length;i++) {
randomNum= (int) (Math.random() * 50);
for(int x=0;x<i;x++) {
if(lottery_numbers[i] == lottery_numbers[x]) {
randomNum= (int) (Math.random() * 50);
x--;
}
}
lottery_numbers[i] = randomNum;
}
/* Loop_4 */
System.out.print("The lottery numbers today are: ");
for(int z : lottery_numbers) {
System.out.print(z + " ");
}
/* Loop_5 */
for(int i=0;i<numbers.length;i++) {
for(int x=0;i<lottery_numbers.length;x++) {
if(numbers[i]==lottery_numbers[x]) {
counter = counter +1;
}
else {
continue;
}
}
}
System.out.println("You got: " + counter + " right ones");
}