Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅重复3个字母最多6次_Java - Fatal编程技术网

Java 仅重复3个字母最多6次

Java 仅重复3个字母最多6次,java,Java,我一直在尝试使用嵌套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++) {

我一直在尝试使用嵌套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;
    }
public静态字符串print3lettersubstring(字符串字){

对于(int len=3;len如果您总是需要三个字符的子字符串,则
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;
}

三个字母还是三个单词?
hed
edg
dge
geh
eho
hog