You are initialising your birthdaypeople
array everytime in each iteration which is why the else
block is never executed because for every j
the birthdaypeople
array is new and have false
value. Take out the array declaration out of the for
loop.
boolean[] birthdaypeople = new boolean[n];
for (int j = 0; j < n; j++) {
int birthday = (int) (Math.random() % n);
if (!birthdaypeople[birthday]) {
birthdaypeople[birthday] = true;
} else {
peopleindex[j + 1]++;
break;
}
}
solved Why the “break” in the loop doesn’t fuction [closed]