Please who know how to randomise number in an array | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Please who know how to randomise number in an array

Array numbers https://code.sololearn.com/cGoxDTJxaVrC/?ref=app

20th Sep 2022, 11:28 AM
Daniel Micheal
Daniel Micheal - avatar
8 Réponses
+ 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
20th Sep 2022, 2:43 PM
Jayakrishna 🇮🇳
+ 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
22nd Sep 2022, 11:25 AM
Bob_Li
Bob_Li - avatar
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..
20th Sep 2022, 2:14 PM
Jayakrishna 🇮🇳
0
It stills bring out error😭
20th Sep 2022, 2:29 PM
Daniel Micheal
Daniel Micheal - avatar
0
I changed all what you told me to change I changed object[] to int[] And i used int j=(int)(Math.random() * (noOfElement));
20th Sep 2022, 2:36 PM
Daniel Micheal
Daniel Micheal - avatar
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];
20th Sep 2022, 2:38 PM
Jayakrishna 🇮🇳
0
Thanks sir
20th Sep 2022, 3:03 PM
Daniel Micheal
Daniel Micheal - avatar
0
You're welcome....
20th Sep 2022, 3:34 PM
Jayakrishna 🇮🇳