I am trying to build a lotto machine using java. This is the code:
import java.util.Scanner;
import java.util.Random;
import java.util.Random;
public class wissam {
public static void main(String[] args) {
int counter = 0;
int[] lottoNumbers = new int[6];
for (int x = 0; x < lottoNumbers.length; x++) {
Random rand = new Random();
lottoNumbers[x] = rand.nextInt(42) + 1;
}
int[] userChoice = new int[6];
System.out.println("Enter a number");
Scanner scan = new Scanner(System.in);
userChoice[0] = scan.nextInt();
for (int y = 1; y < userChoice.length; y++) {
System.out.println("Enter another number");
userChoice[y] = scan.nextInt();
}
for (int x = 0; x < lottoNumbers.length; x++) {
System.out.print(lottoNumbers[x] + " ");
}
System.out.println("");
for (int z = 0; z < userChoice.length; z++) {
for (int a = 0; a < lottoNumbers.length; a++) {
if (lottoNumbers[z] == userChoice[a]) {
counter++;
int b = lottoNumbers[z];
System.out.print("The common numbers were" + b + "");
}
}
}
if (counter == 0) {
System.out.println("You are such a loser");
}
}
}
It asks the user to input 6 numbers and works fine, as long as the user chooses 6 distinct numbers between 1 and 42.
I want to check if the user inputs distinct numbers that are between 1 and 42, and if they aren't I want to ask the user to change the number. How do I do that?