【问题标题】:Why won't my for loop be used in the output?为什么我的 for 循环不在输出中使用?
【发布时间】:2019-01-24 02:46:33
【问题描述】:

这些邮箱的编号是 1 到 150,从 2 号邮箱开始,他打开了所有偶数邮箱的门,其他的都关着。接下来,从 3 号邮箱开始,他每隔三个邮箱就去一次,如果它关着就打开它的门,如果它开着就关上它。然后他每隔四个邮箱重复这个过程,然后每隔五个邮箱重复一次,以此类推。

我正在尝试重新创建此段落。我知道我的第一个和第三个函数已找到,但由于某种原因,我的布尔值没有在输出的第二个函数中使用我的循环。代码如下:

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");
       }
    }
}

【问题讨论】:

  • 欢迎来到 Stackoverflow!您是否可能忘记添加一些代码?除了两个循环之外,您的 doCrazyMailman 方法是空的,如果您希望我们提供帮助,我们需要查看数组索引/分配

标签: java arrays boolean


【解决方案1】:

修改了您的代码以完成作业。 关键是要仔细查看数组索引并了解否定布尔值的用法。

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");
    }
}
}

输出

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

【讨论】:

    猜你喜欢
    • 2022-12-04
    • 1970-01-01
    • 2020-07-07
    • 2018-10-11
    • 2022-01-04
    • 2019-08-20
    • 1970-01-01
    • 2018-07-25
    • 2021-04-24
    相关资源
    最近更新 更多