I'm coding a lottery program, and right now I'm kind of stuck. I let the user pick seven numbers, and at the end I want the program to tell the user which numbers he answered correctly.
I'm having so much trouble understanding arrays that I'm not sure how to store the correctly guessed numbers in an array and then print the elements in the array at the end. I've tried all sorts of variations but nothing is working for me.
package whatevs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class lottery {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int[] userNumbers = new int[7];
int[] winningNumbers = new int[7];
int guesses;
int i;
int counter = 0;
int[]correctGuessed=new int[8];
int x;
ArrayList<Integer> list = new ArrayList<Integer>();
for (x=1; x<40; x++) {
list.add(new Integer(x));
}
Collections.shuffle(list);
for (x=0; x<7;x++) {
winningNumbers[x] = list.get(x);
}
System.out.println("Pick 7 numbers between 1 and 39: ");
for(i = 0; i < 7; i++){
guesses = reader.nextInt();
userNumbers[i] = guesses;
// System.out.println(userNumbers[i]);
for(x = 0; x<7;x++){
if(winningNumbers[x] == userNumbers[i]){
correctGuessed[x] = userNumbers[i];
counter+=1;
}
}
if (counter == 7){
System.out.println("You won!");
}
else
System.out.println("You had " + counter + " numbers correct: " + correctGuessed[x] );
}
}