This question has already been answered:
It is necessary to fill an array of 9 cells with numbers from 1 to 9 so that they do not repeat.
1 option.
int[]ar = new int[9]; ar[0] = (int)(Math.random() * 9 + 1); ar[1] = (int)(Math.random() * 9 + 1); if (ar[1] == ar[0]) { ar[1] = (int)(Math.random() * 9 + 1); } ar[2] = (int)(Math.random() * 9 + 1); if (ar[2] == ar[1] || ar[2] == ar[0]) { ar[2] = (int)(Math.random() * 9 + 1); } ar[3] = (int)(Math.random() * 9 + 1); if (ar[3] == ar[2] || ar[3] == ar[1] || ar[3] == ar[0]) { ar[3] = (int)(Math.random() * 9 + 1); } ar[4] = (int)(Math.random() * 9 + 1); if (ar[4] == ar[3] || ar[4] == ar[2] || ar[4] == ar[1] || ar[4] == ar[0]) { ar[4] = (int)(Math.random() * 9 + 1); } ar[5] = (int)(Math.random() * 9 + 1); if (ar[5] == ar[4] || ar[5] == ar[3] || ar[5] == ar[2] || ar[5] == ar[1] || ar[5] == ar[0]) { ar[5] = (int)(Math.random() * 9 + 1); } ar[6] = (int)(Math.random() * 9 + 1); if (ar[6] == ar[5] || ar[6] == ar[4] || ar[6] == ar[3] || ar[6] == ar[2] || ar[6] == ar[1] || ar[6] == ar[0]) { ar[6] = (int)(Math.random() * 9 + 1); } ar[7] = (int)(Math.random() * 9 + 1); if (ar[7] == ar[6] || ar[7] == ar[5] || ar[7] == ar[4] || ar[7] == ar[3] || ar[7] == ar[2] || ar[7] == ar[1] || ar[7] == ar[0]) { ar[7] = (int)(Math.random() * 9 + 1); } ar[8] = (int)(Math.random() * 9 + 1); if (ar[8] == ar[7] || ar[8] == ar[6] || ar[8] == ar[5] || ar[8] == ar[4] || ar[8] == ar[3] || ar[8] == ar[2] || ar[8] == ar[1] || ar[8] == ar[0]) { ar[8] = (int)(Math.random() * 9 + 1); } it's good that there are only 9 of them.
Option 2.
int[]ar = new int[8]; for (int i = 0; i < ar.length; i++) { i = (int)(Math.random() * 9 + 1); if (ar[i] = ar[i]) { ??? } } In the first version there is a problem: if 2 numbers match, then it changes the second, but if it matches the second time, it does not change. For example, 4-6-5-3 fell 4-6-5-3 and 6 came, he would change it, for example, by 4 , and 4 no longer change. How to do?
I don’t have to decide for me, please give me a thought.