mardi 24 avril 2018

Mailbox Program Java (flipping booleans)

I'm tasked with creating a program where the user enters a number, and that will be the total number of mailboxes. All the mailboxes will start in a closed position (boolean true). Then you will flip every other mailbox, then third then fourth then fifth and so on until you get to the last mailbox being the only one flipped. Then you must report back which mailboxes remain closed. I haven't got to return to the user the closed boxes yet. As I am stuck on how to get it to filter through the array.

    scanner  input = new Scanner(System.in);
    System.out.println("This program will close and open mailboxes in a fixed pattern.  How many mailboxes would you like to see?");
    int numOfBoxes = input.nextInt();
    boolean [] boxes = new boolean [numOfBoxes];
    for(int i = 0; i <= numOfBoxes; i++){
      for(int j = 2; j <= numOfBoxes; j++){
        if(boxes.indexOf[i] % j == 0){
           boxes[i] = !boxes[i];
        }
      }
    }

Aucun commentaire:

Enregistrer un commentaire