I have a problem in generating the non-duplicate numbers. I tried to do the do-while loop but it seems not working. How do I fix it?
import java.util.Arrays;
public class Assignment2_Q2 {
public static void main (String[] args){
int[] myList = new int[10];
number(myList);
sortedOrder(myList);
display (myList);
}
public static void number(int[] list){
int random;
random = (int)(Math.random() * 21);
list[0] = random;
for (int i = 1; i < list.length; i++){
do{
random = (int)(Math.random() * 21);
list[i] = random;
}while(list[i] == list[i-1]);
}
}
public static void sortedOrder(int[] list){
java.util.Arrays.sort(list);
}
public static void display(int[] list){
System.out.println("The array in the sorted order:\n" + Arrays.toString(list) + "\n");
}
}
Aucun commentaire:
Enregistrer un commentaire