int matrice2 [][] = new int [5][5];
for (int i=0;i<5;i++) {
System.out.println(" ");
for(int j=0;j<5;j++) {
matrice2[i][j] = (int)(Math.random()*10);
System.out.print(" "+matrice2[i][j]+" ");
}
}
System.out.println(" ");
System.out.println(" ");
for (int i=0;i<5;i++) {
System.out.println(" ");
for(int j=0;j<5;j++) {
for (int k=0;k<5;k++) {
if(j!=k) {
if (matrice2[i][j]==matrice2[i][k]) {
matrice2[i][k]=(int)(Math.random()*10);
}
}
}
System.out.print(" "+matrice2[i][j]+" ");
}
}
I wanted to make a multidimensional array without having any repeated numbers in the same row so the column doesn't matter if it has repeating numbers.
What I did here is generate 5x5 arrays formed in square. Having index "j" as the one that gets compared with numbers in index "k" that checks the whole row if there are numbers that is equal to the number it contains in index "j". So my only problem is that after detecting the number in "k" that is equal to the number in "j", the number in index "k" will generate a new number replacing the current number in "k" but the result in console shows that the original 5x5 array which is the first formed of the array before it gets changed has been changed but the numbers that have been replaced generated a numbers that already exist even it should keep changing until "k" cannot detect any repeated numbers.
I can think of other ways of doing it but I really want to know why it doesn't work even though I really think that it shouldn't give any problems but I might have missed something.
here's is the example.
9 1 3 8 4
5 3 2 4 8
9 8 5 6 5
6 3 0 8 7
2 8 6 3 9
9 1 3 8 4
5 3 2 4 8
9 8 5 6 9
6 3 0 8 7
2 8 6 3 9
it shouldn't happen because "k" should have seen it since he starts from index 0 to 4.
Aucun commentaire:
Enregistrer un commentaire