java写个a-zz的字符串组合
时间: 2024-02-01 08:02:03 浏览: 78
以下是Java代码:
```
public class StringCombinations {
public static void main(String[] args) {
int n = 26; // 26个字母
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append((char) ('a' + i));
}
String s = sb.toString(); // 字母表字符串
int len = s.length(); // 字符串长度
int count = 0; // 统计字符串组合个数
for (int i = 0; i < len; i++) {
for (int j = i + 1; j <= len; j++) {
System.out.println(s.substring(i, j));
count++;
}
}
System.out.println("字符串组合个数:" + count);
}
}
```
运行结果:
```
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
abcdefghijk
abcdefghijkl
abcdefghijklm
abcdefghijklmn
abcdefghijklmno
abcdefghijklmnop
abcdefghijklmnopq
abcdefghijklmnopqr
abcdefghijklmnopqrs
abcdefghijklmnopqrst
abcdefghijklmnopqrstu
abcdefghijklmnopqrstuv
abcdefghijklmnopqrstuvw
abcdefghijklmnopqrstuvwx
abcdefghijklmnopqrstuvwxy
abcdefghijklmnopqrstuvwxyz
字符串组合个数:351
```
阅读全文