I want to generate two numbers in a specified list of numbers. I wrote this code but it does not work. Can anyone suggest a better solution?
public classl {
public static void main(String[] args) {
int[] lottery = new int[2];
int randomNum;
for (int i = 0; i < 3; i++) {
randomNum = (int) (Math.random() * (3,9,25,41,43,50,68)); // Random number created here.
for (int x = 0; x < i; x++) {
if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
{
randomNum = (int) (Math.random() * (3,9,25,41,43,50,68));// If random number is same, another number generated.
x = -1; // restart the loop
}
}
lottery[i] = randomNum;
}
for (int i = 0; i < lottery.length; i++)
System.out.print(lottery[i] + " ");
}
}
}
Aucun commentaire:
Enregistrer un commentaire