package dayo4;
import java.util.Random;
public class Arraychouj01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random ss=new Random();
int s;
int[]a=new int[6];
for(int i=0;i<6;i ){
s=ss.nextInt(7) 1;
System.out.println(s);
if(i>0){
for(int j=0;j<i;j ){
if(s==a[j]){
i--;
break;
}
else{
a[i]=s;
break;
}
}
}else{
a[0]=s;
}
}
for(int z=0;z<6;z ){
System.out.print(a[z] " ");
}
}
}
The code above means to randomly pick a number from 1 to 7 and then pick 6 without duplication. The problem is the break after else; if you add it, it is wrong. If you don’t add it, it is right. In my understanding, break means jumping out of the current loop. Meaning: So when it is found that the number is not repeated, assign a value and then jump out of the loop and go to the next loop. However, if brake is added, duplicate arrays will appear. I don’t know the application of break very well
nearest2017-07-25 11:36:04
Break only loops to one non-repeating data. break should be removed. Get all unique data