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