好的,所以这有点棘手,但让我解释一下。我有100个值的布尔值。对于100步,我必须将值从true切换为false,但这是步长值的增量。 e.g。
第一步=递增1并将false翻转为true 第二步=递增2并翻转布尔值(无论值) 第三步........我相信你现在得到这个。这将逐步增加100步,每次增加步长值。
所以我到目前为止已经得到了一些狡猾的结果,我需要知道如何查看值或布尔值并仅在最后一步之后显示真实的值。任何帮助?
/
public class MailBox {
public static void main(String[] args) {
boolean[] amountToflip = new boolean[100];
int boxnum;
for(boxnum=0; boxnum<amountToflip.length;boxnum++){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=1; boxnum<amountToflip.length;boxnum+=2){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=2; boxnum<amountToflip.length;boxnum+=3){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=3; boxnum<amountToflip.length;boxnum+=4){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=4; boxnum<amountToflip.length;boxnum+=5){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=5; boxnum<amountToflip.length;boxnum+=6){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=6; boxnum<amountToflip.length;boxnum+=7){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=7; boxnum<amountToflip.length;boxnum+=8){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=8; boxnum<amountToflip.length;boxnum+=9){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=9; boxnum<amountToflip.length;boxnum+=10){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=10; boxnum<amountToflip.length;boxnum+=11){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=11; boxnum<amountToflip.length;boxnum+=12){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=12; boxnum<amountToflip.length;boxnum+=13){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=13; boxnum<amountToflip.length;boxnum+=14){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=14; boxnum<amountToflip.length;boxnum+=15){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=15; boxnum<amountToflip.length;boxnum+=16){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=16; boxnum<amountToflip.length;boxnum+=17){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=17; boxnum<amountToflip.length;boxnum+=18){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=18; boxnum<amountToflip.length;boxnum+=19){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=19; boxnum<amountToflip.length;boxnum+=20){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=20; boxnum<amountToflip.length;boxnum+=21){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=21; boxnum<amountToflip.length;boxnum+=22){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=22; boxnum<amountToflip.length;boxnum+=23){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=23; boxnum<amountToflip.length;boxnum+=24){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=24; boxnum<amountToflip.length;boxnum+=25){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=25; boxnum<amountToflip.length;boxnum+=26){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=26; boxnum<amountToflip.length;boxnum+=27){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=27; boxnum<amountToflip.length;boxnum+=28){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=28; boxnum<amountToflip.length;boxnum+=29){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=29; boxnum<amountToflip.length;boxnum+=30){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=30; boxnum<amountToflip.length;boxnum+=31){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=31; boxnum<amountToflip.length;boxnum+=32){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=32; boxnum<amountToflip.length;boxnum+=33){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=33; boxnum<amountToflip.length;boxnum+=34){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=34; boxnum<amountToflip.length;boxnum+=35){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=35; boxnum<amountToflip.length;boxnum+=36){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=36; boxnum<amountToflip.length;boxnum+=37){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=37; boxnum<amountToflip.length;boxnum+=38){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=38; boxnum<amountToflip.length;boxnum+=39){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=39; boxnum<amountToflip.length;boxnum+=40){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=40; boxnum<amountToflip.length;boxnum+=41){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=41; boxnum<amountToflip.length;boxnum+=42){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=42; boxnum<amountToflip.length;boxnum+=43){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=43; boxnum<amountToflip.length;boxnum+=44){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=44; boxnum<amountToflip.length;boxnum+=45){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=45; boxnum<amountToflip.length;boxnum+=46){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=46; boxnum<amountToflip.length;boxnum+=47){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=47; boxnum<amountToflip.length;boxnum+=48){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=48; boxnum<amountToflip.length;boxnum+=49){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=49; boxnum<amountToflip.length;boxnum+=50){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=50; boxnum<amountToflip.length;boxnum+=51){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=51; boxnum<amountToflip.length;boxnum+=52){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=52; boxnum<amountToflip.length;boxnum+=53){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=53; boxnum<amountToflip.length;boxnum+=54){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=54; boxnum<amountToflip.length;boxnum+=55){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=55; boxnum<amountToflip.length;boxnum+=56){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=56; boxnum<amountToflip.length;boxnum+=57){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=57; boxnum<amountToflip.length;boxnum+=58){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=58; boxnum<amountToflip.length;boxnum+=59){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=59; boxnum<amountToflip.length;boxnum+=60){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=60; boxnum<amountToflip.length;boxnum+=61){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=61; boxnum<amountToflip.length;boxnum+=62){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=62; boxnum<amountToflip.length;boxnum+=63){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=63; boxnum<amountToflip.length;boxnum+=64){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=64; boxnum<amountToflip.length;boxnum+=65){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=65; boxnum<amountToflip.length;boxnum+=66){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=66; boxnum<amountToflip.length;boxnum+=67){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=67; boxnum<amountToflip.length;boxnum+=68){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=68; boxnum<amountToflip.length;boxnum+=69){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=69; boxnum<amountToflip.length;boxnum+=70){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=70; boxnum<amountToflip.length;boxnum+=71){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=71; boxnum<amountToflip.length;boxnum+=72){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=72; boxnum<amountToflip.length;boxnum+=73){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=73; boxnum<amountToflip.length;boxnum+=74){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=74; boxnum<amountToflip.length;boxnum+=75){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=75; boxnum<amountToflip.length;boxnum+=76){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=76; boxnum<amountToflip.length;boxnum+=77){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=77; boxnum<amountToflip.length;boxnum+=78){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=78; boxnum<amountToflip.length;boxnum+=79){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=79; boxnum<amountToflip.length;boxnum+=80){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=80; boxnum<amountToflip.length;boxnum+=81){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=81; boxnum<amountToflip.length;boxnum+=82){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=82; boxnum<amountToflip.length;boxnum+=83){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=83; boxnum<amountToflip.length;boxnum+=84){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=84; boxnum<amountToflip.length;boxnum+=85){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=85; boxnum<amountToflip.length;boxnum+=86){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=86; boxnum<amountToflip.length;boxnum+=87){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=87; boxnum<amountToflip.length;boxnum+=88){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=88; boxnum<amountToflip.length;boxnum+=89){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=89; boxnum<amountToflip.length;boxnum+=90){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=90; boxnum<amountToflip.length;boxnum+=91){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=91; boxnum<amountToflip.length;boxnum+=92){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=92; boxnum<amountToflip.length;boxnum+=93){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=93; boxnum<amountToflip.length;boxnum+=94){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=94; boxnum<amountToflip.length;boxnum+=95){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=95; boxnum<amountToflip.length;boxnum+=96){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=96; boxnum<amountToflip.length;boxnum+=97){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=97; boxnum<amountToflip.length;boxnum+=98){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=98; boxnum<amountToflip.length;boxnum+=99){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
for(boxnum=99; boxnum<amountToflip.length;boxnum+=100){
amountToflip[boxnum] = !amountToflip[boxnum];
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
}
}
结果打印输出显示每个步骤的值,包括true和false,我只想显示真值。我把一个int放在这里,这样我就可以有一个盒子号码,所以我知道哪个号码是真的,例如方框1 =真,方框7 =真
答案 0 :(得分:2)
请记住,为了避免任何多余的工作,我们使用循环!那么为什么100个循环做同样的事情只改变boxnum
的值?
试试这个以避免100次循环:
int i;
for(i = 0; i <100; i++) {
for(boxnum=i; boxnum < amountToflip.length; boxnum = boxnum + 1 +i){
amountToflip[boxnum] = !amountToflip[boxnum];
}
}
现在打印出类似的东西,
for(boxnum = 0; boxnum < amountToflip.length; boxnum++) {
if(amountToflip[boxnum]) // this condition will check only for true
System.out.println("Mailbox: " + boxnum + amountToflip[boxnum]);
}
答案 1 :(得分:1)
以下是您的代码已修复,因此您不必要don't repeat yourself:
public static void mailbox() {
boolean[] amountToflip = new boolean[100];
for (int boxNum = 0; boxNum < amountToflip.length; boxNum++) {
for (int i = boxNum; i < amountToflip.length; i++) {
amountToflip[i] = !amountToflip[i];
System.out.println("Mailbox: " + i + amountToflip[i]);
}
}
}
现在你可以看到只打印布尔值为true
的地方是多么容易:
public static void mailbox() {
boolean[] amountToflip = new boolean[100];
for (int boxNum = 0; boxNum < amountToflip.length; boxNum++) {
for (int i = boxNum; i < amountToflip.length; i++) {
amountToflip[i] = !amountToflip[i];
if (amountToflip[i]) {
System.out.println("Mailbox: " + i + amountToflip[i]);
}
}
}
}
这是相同的代码,打印结束并整理:
public static void mailbox() {
boolean[] amountToflip = new boolean[100];
for (int boxNum = 0; boxNum < amountToflip.length; boxNum++) {
for (int i = boxNum; i < amountToflip.length; i++) {
amountToflip[i] = !amountToflip[i];
}
}
// Roll all 100 up into a single display.
for (int i = 0; i < amountToflip.length; i++) {
System.out.print(amountToflip[i] ? "1" : " ");
}
System.out.println();
}
打印:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
我仍然不明白你想要达到的目标。
答案 2 :(得分:1)
为了做到这一点,你想要按照描述进行翻转,然后只显示真实方框的结果吗?然后我认为这就是你要找的东西:
public class MailBox {
public static void main(String[] args) {
boolean[] amountToflip = new boolean[100];
// Perform the flipping
for (int increment=1; increment <= amountToflip.length; increment++) {
for (int boxnum=increment - 1; boxnum < amountToflip.length; boxnum+=increment) {
amountToflip[boxnum] = !amountToflip[boxnum];
}
}
// Now print the result
for (int i = 0; i < amountToflip.length; i++) {
if (amountToflip[i]) {
System.out.println("Mailbox: " + i);
}
}
}
}