我一直在尝试使用嵌套的for循环来重复单词" hedgehog“,只有三个字母,最多6次,但是,它一直在重复,直到它完全拼写出hedgehog。
public static String print3LetterSubstrings(String word) {
for (int len = 3; len <= word.length(); len++) {
for (int i = 0; i+len <= word.length(); i++) {
System.out.println(word.substring(i, i+len));
}
}
return word;
}我想要的是只有3个字母,重复6次。
hed edg dge geh eho hog
发布于 2019-08-12 09:10:10
如果你总是想要三个字符子串,那么len不应该改变。您希望循环打印从i到i + len的子字符串。喜欢,
public static String print3LetterSubstrings(String word) {
int len = 3;
for (int i = 0; i + len <= word.length(); i++) {
System.out.println(word.substring(i, i + len));
}
return word;
}作为print3LetterSubstrings("hedgehog");调用时的输出(根据请求)
hed
edg
dge
geh
eho
hoghttps://stackoverflow.com/questions/57454675
复制相似问题