+ 1
Please who know how to randomise number in an array
Array numbers https://code.sololearn.com/cGoxDTJxaVrC/?ref=app
8 Respostas
+ 1
public class Teams {
public static void shuffle (int[] Array){ //1
int noOfElement = Array.length;
for (int i=0; i<noOfElement; i++){
int j=(int)(Math.random() * (noOfElement)); // 2
int temp = Array[j]; //3
Array[j] = Array[i];
Array[i] = temp;
}
}
//} //4
public static void main(String[]args){ //5
int [] noOfTeams = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
Teams.shuffle(noOfTeams);
for (int i = 0; i<noOfTeams.length; i++){
System.out.print(noOfTeams[i] + ","); //adding
}
}
} //close here
+ 1
Daniel Micheal
if you don't absolutely have to use plain old arrays, I would suggest importing List, Arrays and Collections from java.utils
It's been a long time since I've coded in Java, so the formatting might be odd.đ
https://code.sololearn.com/c4hzO4YnY7wB/?ref=app
0
You are not declared main method correctly..
And object is undefined.., if you mean Object[] then why you need, it need casting properly... Take int[] instead.
And your random generates out of indexes.. Use
int j=(int)(Math.random() * (noOfElement));
Then it works fine.. Hope it helps..
0
It stills bring out errorđ
0
I changed all what you told me to change
I changed object[] to int[]
And i used int j=(int)(Math.random() * (noOfElement));
0
Main method must be
public static void main( String[] ar) {
//..
}
and it must be inside a class. You are wrote it out side class.. add { } braces properly..
and also take
int temp = arr[j]; instead of
Object temp = arr [j];
0
Thanks sir
0
You're welcome....