1public class Test {
2 public static void main(String[] args) {
3 // Declare a constant value for the number of lockers
4 final int NUMBER_OF_LOCKER = 100;
5
6 // Create an array to store the status of each array
7 // The first student closed all lockers, each lockers[i] is false
8 boolean[] lockers = new boolean[NUMBER_OF_LOCKER];
9
10 // Each student changes the lockers
11 for (int j = 1; j <= NUMBER_OF_LOCKER; j++) {
12 // Student Sj changes every jth locker
13 // starting from the lockers[j - 1].
14 for (int i = j - 1; i < NUMBER_OF_LOCKER; i += j) {
15 lockers[i] = !lockers[i];
16 }
17 }
18
19 // Find which one is open
20 for (int i = 0; i < NUMBER_OF_LOCKER; i++) {
21 if (lockers[i])
22 System.out.println("Locker " + (i + 1) + " is open");
23 }
24 }
25}