I wrote down my code at first and then it cannot ensure that it is 6 different number.
public class JavaTest{
public static void main(String[] args){
int[] list = new int[6];
int number = (int)(Math.random() * 10 + 1);
for (int i = 0; i < list.length; i++){
list[i] = number;
number = (int)(Math.random() * 10 + 1);
for (int j = 1; j < i; j++){
number = (int)(Math.random() * 10 + 1);
if (number == list[j])
number = (int)(Math.random() * 10 + 1);
}
}
for (int i = 0; i < list.length; i++){
System.out.print(list[i] + " ");
}
}
}
I think something wrong with my inner for loop j, but I don't know how to improve it. Can anyone help me modify it? Thanks a lot!!