这可以用递归解决不过我忘记咋个写了,用了for嵌套
- public class Test {
- public static void main(String args[]) {
- String[] s = { "a", "b", "c" };
- for (int a = 1; a <= 3; a++) {
- if (a == 1) {
- System.out.println(s[0] + " " + s[1] + " " + s[2]);
- }
- if (a == 2) {
- for (int j = 0; j < s.length; j++) {
- for (int k = 0; k < s.length; k++) {
- if (j != k) {
- System.out.print(s[j] + s[k] + " ");
- }
- }
- }
- System.out.println();
- }
- if (a == 3) {//判断该数是否由三个字符构成
- for (int j = 0; j < s.length; j++) {
- for (int k = 0; k < s.length; k++) {
- for (int n = 0; n < s.length; n++) {
- if (j != k && j != n && k != n) {
- System.out.print(s[j] + s[k] + s[n]);
- System.out.print(" ");
- }
- }
- }
- }
- }
- }
- }
- }
复制代码 |