简体   繁体   中英

Mailman mailbox problem using 2d arrays and for loops

This program is supposed to create a boolean array with 150 "mailboxes". The first for loop is supposed to set them all to "closed" defined by false. The second for loop should start at the second mailbox, index[1] and open every even. The third and fourth for loops are supposed to start at the third mailbox index[2] and open every third then after reaching the 150th return to the fourth for loop and open every third. The final for loop is supposed to print out which for loops are closed. I haven't been able to get it to run past line 17.

public class CS2LAB1 {
public static void main(String[] args)
{

    boolean[] mailboxes = new boolean[149];

    /*this function sets all mailbox doors to closed*/
    for(int i = 0; i <= mailboxes.length; i++)
    {
     mailboxes[i] = false;
    }

    /*This function uses a for loop to begin at mailbox 2 and open every even mailbox
    */
    for(int count = 1; count <= mailboxes.length; count = count + 2)
    {
     mailboxes[count] = true;
    }

    /*This function starts at 3 and opens every 3rd mailbox, next it goes to 4 and opens every 3rd mailbox followed by the 4th then 5th then 6th */
    for(int count2 = 2; count2<=mailboxes.length; count2++)
    {
        for(int count3 = count2; count3 <= mailboxes.length; count3 = count3 + count2)
        {
          if(mailboxes[count2] = true)
          { mailboxes[count2] = false;}
          else{ mailboxes[count2] = true;}    
        }
    }

    /*This function outputs all the closed doors*/
    for(int print =0; mailboxes.length>=print; print++)
        if(mailboxes[print] = false)
        {
            System.out.println("Mailbox" + print++ + "is closed" );
        }
}

}

Update: I went through and changed the boolean values so that closed is now equivalent to true and open is for false off the basis that any boolean array is already set to false . I did not get the correct output instead the program outputs all even mailboxes as opened

moreover I believe I've narrowed the problem down to the nested for loop. The if statements within are never used and I do not understand why. Any ideas?

    for(int count2 = 2; count2<= 149; count2++)
    {
        for(int count3 = 2; count3 <= 149; count3 = count3 + 3)
        {
          if(mailboxes[count3] = false)
          { mailboxes[count3] = true;}
          else{ mailboxes[count3] = false;}    
        }
    }

Your first for loop is probably throwing an IndexOutOfBoundException . Array's in Java are 0-indexed meaning that they start at 0 and go all the way up until Array.length - 1 . Therefore in order to traverse an entire array one would iterate from for(int i = 0; i < arr.length; i++) .

Your for loop is traversing from for(int i = 0; i <= mailboxes.length; i++) . Meaning the last value for index i is 149 when the arrays indexes go from [0, ..., 148] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM