Почему мой цикл for не будет использоваться в выводе? - PullRequest
0 голосов
/ 24 января 2019

Эти почтовые ящики были пронумерованы с 1 по 150, и, начиная с почтового ящика 2, он открыл двери всех чётных почтовых ящиков, оставив остальные закрытыми.Затем, начиная с почтового ящика 3, он переходил к каждому третьему почтовому ящику, открывая дверь, если она была закрыта, и закрывая ее, если она была открыта.Затем он повторил эту процедуру для каждого четвертого почтового ящика, затем для каждого пятого почтового ящика и т. Д.

Я пытаюсь воссоздать этот абзац.Я знаю, что моя первая и третья функция находятся в find, но по какой-то причине мой логический параметр не использует мой цикл во второй функции в выводе.Вот код:

public class Lab {

    public static void main (String[] args) {

        Boolean[] mailboxarray = new Boolean[150];

        closeMailboxes(mailboxarray);
        doCrazyMailman(mailboxarray);
        showMailboxstate(mailboxarray);
    }

    /** 
     * purpose: 
     * pre-condition: 
     * post-condition:
     * @param mailboxarray
     */
    public static void closeMailboxes(Boolean[] mailboxarray) {
        for (int i = 0; i <150; i++) {
            mailboxarray[i] = Boolean.FALSE;
        }
    }

    /** 
     * purpose: 
     * pre-condition: 
     * post-condition:
     * @param mailboxarray
     */
    public static void doCrazyMailman(Boolean[] mailboxarray) {
        // to help you with troubleshooting, I will add some outputs
        // it is always beneficial to be able to see what's your program
        // is actually doing right now
        for (int i = 1; i <= 150; i++) {
           for (int j = i; j < 150;j=j+i+1) {











                        }
        }
    }

    /** 
     * purpose: 
     * pre-condition: 
     * post-condition:
     */
    public static void showMailboxstate(Boolean[] mailboxarray) {
       for (int i = 0; i < 150; i++) {
           int number = i + 1;

           // this will output only closed doors 
           // as shown in assignment's screenshot
           // it reads next: 
           // if the current boolean is FALSE - display message
           if (!mailboxarray[i])


              System.out.println("Door " + number +  " is closed");
       }
    }
}

1 Ответ

0 голосов
/ 24 января 2019

Адаптировал ваш код для выполнения домашней работы.Задача состоит в том, чтобы внимательно посмотреть на индексы массива и знать, как использовать отрицательные логические значения.

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

    Boolean[] mailboxarray = new Boolean[150];

    closeMailboxes(mailboxarray);
    doCrazyMailman(mailboxarray);
    showMailboxstate(mailboxarray);
}

/**
 * purpose: pre-condition: post-condition:
 * 
 * @param mailboxarray
 */
public static void closeMailboxes(Boolean[] mailboxarray) {
    for (int i = 0; i < 150; i++) {
        mailboxarray[i] = Boolean.FALSE;
    }
}

/**
 * purpose: pre-condition: post-condition:
 * 
 * @param mailboxarray
 */
public static void doCrazyMailman(Boolean[] mailboxarray) {
// to help you with troubleshooting, I will add some outputs
// it is always beneficial to be able to see what's your program
// is actually doing right now
    for (int i = 2; i <= 150; i++) {
        for (int j = i; j <= 150; j = i + j) {
            // switch open-close by negate
            // work with real case counter and take care to modify at proper place in arr
            mailboxarray[j - 1] = !mailboxarray[j - 1];
            // System.out.println(i + ":" + j + ":" + !mailboxarray[j - 1] + ":" + 
            // mailboxarray[j - 1]);
        }
    }
}

/**
 * purpose: pre-condition: post-condition:
 */
public static void showMailboxstate(Boolean[] mailboxarray) {
    for (int i = 0; i < 150; i++) {
        int number = i + 1;

// this will output only closed doors
// as shown in assignment's screenshot
// it reads next:
// if the current boolean is FALSE - display message
        if (!mailboxarray[i])

            System.out.println("Door " + number + " is closed");
    }
}
}

output

Door 1 is closed
Door 4 is closed
Door 9 is closed
Door 16 is closed
Door 25 is closed
Door 36 is closed
Door 49 is closed
Door 64 is closed
Door 81 is closed
Door 100 is closed
Door 121 is closed
Door 144 is closed
...