【发布时间】:2020-05-16 17:44:41
【问题描述】:
我需要在 Java 中完成以下工作:
1. 取一个字符串。
2. 生成字符串中所有2个连续的字符。
3. 生成字符串中所有 3 个连续的字符。
4. 以此类推,直到最后一代,将是 (str.length())-1-连续字符。
为了进一步澄清,请考虑字符串hello!。该字符串的长度为6。注意最后一代是 5 个连续的字符。输出应该是:
he // 2-consecutive
el // 2-consecutive
ll // 2-consecutive
lo // 2-consecutive
o! // 2-consecutive
hel // 3-consecutive
ell // 3-consecutive
llo // 3-consecutive
lo! // 3-consecutive
hell // 4-consecutive
ello // 4-consecutive
llo! // 4-consecutive
hello // 5-consecutive
ello! // 5-consecutive
这是我尝试过的:
String str = "hello!";
int len = str.length();
for (int set = 2; set <= (len-1); set++) {
for (int n = 0; n <= (len-1); n++) {
for (int k = 0; k <= n; k++) {
System.out.print(str.charAt(k));
}
System.out.println();
}
}
代码给出了以下输出,这与我之前希望的完全不同:
h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!
This question 会帮助我,但我完全不熟悉 ruby。
【问题讨论】:
标签: java string combinations