I'm trying to make a long array composed of the digits 0 - 9 in a random order, meaning there would be no duplicates of the same digit. I'm a novice coder, and this is what I tried to come up with.
public static void shuffle()
{
long[] rn = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Random rand = new Random();
for (int i = 0; i < 10; i++) {
int n;
n = rand.nextInt(10) + 0;
for (int j = 0; j < 10; j++)
{
if (n != rn[j])
{
j++;
}
else if (n == rn[j])
{
n = rand.nextInt(10) + 0;
if (j > 0)
{
j--;
}
}
}
rn[i] = n;
}
for (int l = 0; l < 10; l++)
{
System.out.print(rn[l]);
}
System.out.println();
}
By my understanding, this shouldn't let any duplicate digits pass, yet it does. Did I do something wrong?
PS: I'm not allowed to use collections or array lists. I'm using the Ready To Program IDE which apparently doesn't support those.
Aucun commentaire:
Enregistrer un commentaire