A very simple fix - replace u++;
with u--;
. ++
will make the loop stop, --
will make it carry on.
Though I'd suggest something more like the below. I hope it's easy enough to understand.
Integer[] dizi = new Integer[6];
for (int j = 0; j < 6; j++)
{
boolean isValid;
do
{
dizi[j] = (int) ((Math.random() * 6) + 1);
isValid = true;
for (int k = 0; isValid && k < j; k++)
if (dizi[k] == dizi[j])
isValid = false;
}
while (!isValid);
dizi_asil[i][j] = dizi[j];
}
I'd also suggest the Random class, which has a nextInt(int)
method, which is better than (int) ((Math.random() * 6) + 1)
.
But shuffling is probably a faster way to do it. Either use the API like one of the other answers or look into the Fisher-Yates / Knuth shuffle for an easy shuffle algorithm.