这些邮箱从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");
}
}
}
答案 0 :(得分:0)
调整您的代码以执行作业。 所要抓住的是仔细查看数组索引,并知道使用否定布尔值。
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