我正在为我的 CS 期末练习一些练习,并且被困在这个问题上,我必须读取一个字符串,从用户那里获得最小长度,并返回至少有那么多字母的单词数量。似乎我的代码很好,但它无法打印出答案。谁能帮我吗?
public class WordCount {
public static void main (String [] args) {
System.out.println("Enter a string: ");
String input = IO.readString();
System.out.println("Enter minimum word length");
int wordlength = IO.readInt();
int count = 0 ;
do {
for (int i = 0 ; i < input.length(); i ++) {
if (input.indexOf(i) == ' ') {
String check = input.substring(0, i);
if (check.length() >= wordlength) {
count++;
input = input.substring(i);
break;
}
}
}
} while (input.length() > 0);
System.out.print("Words longer than " + wordlength + " characters: " + count);
}
}
似乎while循环无限运行,但我不知道为什么!