if文のネストでのelseは好きではない その続き 2007/06/11
2008/03/23

boolean値が3つあって、その状態の組み合わせてをelseなしで記述するための準備。


public class PrintCombination2 {

public PrintCombination2() {

}

public static void main(String[] args) {
p(3);
}

public static void p(int n) {

int t = 1;
for (int i = 0; i < n; i++)
t = t * 2;

System.out.println("c:" + t);
Boolean[] b = new Boolean[n];
for (int i = 0; i < n; i++)
b[i] = false;

int pos = n - 1;
pp(b, pos);
}

public static void pp(Boolean[] bb, int pos) {
if (!(0 <= pos)) {
print(bb);
return;
}

bb[pos] = true;
pp(bb, pos - 1);

bb[pos] = false;
pp(bb, pos - 1);

}

public static void print(Boolean[] bs) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bs.length; i++)
builder.append(bs[i] ? "T" : "F").append(" ");

// System.out.println(builder);
StringBuilder builder2 = new StringBuilder();
for (int i = 0; i < bs.length; i++)
builder2.append(
bs[i] ? (char)(97 + i) : "not(" + (char)(97 + i) + ")")
.append(i < bs.length - 1 ? " && " : "");
System.out.println("if(" + builder2 + "){");
System.out.println("// " + builder);
System.out.println("return;");
System.out.println("}");
}

}




c:8
if(a && b && c){
// T T T
return;
}
if(not(a) && b && c){
// F T T
return;
}
if(a && not(b) && c){
// T F T
return;
}
if(not(a) && not(b) && c){
// F F T
return;
}
if(a && b && not(c)){
// T T F
return;
}
if(not(a) && b && not(c)){
// F T F
return;
}
if(a && not(b) && not(c)){
// T F F
return;
}
if(not(a) && not(b) && not(c)){
// F F F
return;
}


ちなみにnotメソッドは、


static boolean not(boolean b){
return !b;
}



ここまでして、if-elseの山形を消したいのか。。。

: