public static void combinations(String s) {
char[] original = s.toCharArray();
int original_size = s.length();
String temp = new String();
for (int i = 0; i < original_size; i++) {// add the first element
String sb = "";
temp = "";
sb = "" + original[i];
temp = sb.toString();
System.out.println(sb);
for (int j = i + 1; j < original_size; j++) {// add the other
// element in the
// array
if (i == j)
continue;
sb = temp + "" + original[j];
System.out.println(sb);
}
// sb = "";
}
}
public static void main(String[] args) {
combinations("abc");
}
結果應該是: A,AB,AC,ABC,B,BC,C打印出一個字符串的全部可能的組合
但我的計劃是: A,AB,AC, b,bc,c。我不能打印出ABC
請首先在代碼中修改縮進。 –
你描述的輸出不是「所有可能的組合」,而是「所有可能的子串」,這是完全不同的東西(保留字符順序)。 – alfasin
建議:由於你有預期的輸出,並且程序沒有返回預期的輸出,所以在代碼中的適當的位置放置一個斷點(例如在for循環的第二個開始處),並且看看爲什麼abc永遠不會將通過逐行執行程序來生成。 – NYCdotNet