Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: A puzzle exercise.

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default A puzzle exercise.

    So here's the question:

    (Game: locker puzzle) A school has 100 lockers and 100 students. All lockers are
    closed on the first day of school. As the students enter, the first student, denoted
    S1, opens every locker. Then the second student, S2, begins with the second
    locker, denoted L2, and closes every other locker. Student S3 begins with the
    third locker and changes every third locker (closes it if it was open, and opens it if
    it was closed). Student S4 begins with locker L4 and changes every fourth locker.
    Student S5 starts with L5 and changes every fifth locker, and so on, until student
    S100 changes L100.
    After all the students have passed through the building and changed the lockers,
    which lockers are open? Write a program to find your answer and display all
    open locker numbers separated by exactly one space.
    (Hint: Use an array of 100 Boolean elements, each of which indicates whether a
    locker is open (true) or closed (false). Initially, all lockers are closed.)


    And my logic did this:

    public class Exercise_7_23 {
      public static void main(String[] args) {
     
        boolean[] lock = new boolean[100];
     
        for(int i = 0; i < lock.length; i++) {              //Initializing all array values with false
          lock[i] = false;
        }
     
        for(int i = 0; i < 100; i++) {             //Students
          for(int j = 0; j < 100; j += i) {        //Lockers
     
            if(lock[j] == true) {        //If open
              lock[j] = false;          //close
            } else {
              lock[j] = true;     //open
            }
     
          }
        }
     
        System.out.println("The lockers open are");
     
        for(int i = 0; i < lock.length; i++) {            //Printing array number if locker is open.
          if(lock[i] == true) {                        
            System.out.print((i+1) + " ");
          }
        }
      }
    }

    but after running it, it doesn't load, just frozen.

    I'd assume from the loop but it isn't that bad ? i think.

    --- Update ---

    I refuse to look at the answer and i'd just appreciate some hints.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A puzzle exercise.

    it doesn't load, just frozen.
    Does that mean the program goes into a loop and keeps executing without stopping?

    Try debugging the code by adding some print statements that track the execution progress and show the values of the variables that control the loops.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    UniverseCloud (October 21st, 2019)

  4. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: A puzzle exercise.

    I could barely see it..

    I changed the outer loop to be (int i = 1; i <= 100; i++).

    works like a charm now.

    thanks!

    P.S i got to add, adding print statements helped a lot : i didn't know this, so thanks again.
    Last edited by UniverseCloud; October 21st, 2019 at 01:38 PM.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. 15 square puzzle
    By Yamaha360 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 10th, 2013, 02:10 PM
  4. Puzzle Game. Need some help
    By clydefrog in forum Java Theory & Questions
    Replies: 10
    Last Post: March 12th, 2012, 03:14 PM
  5. JavaScript pic puzzle
    By fr334a11 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2011, 08:29 AM