无法弄清楚我的代码有什么问题

Can't figure out what is wrong with my code

这是我从老师那里得到的指示:

在文件 WordCount.java 中写入您的代码。您的代码应该进入具有以下签名的方法。您可以编写自己的主要方法来测试您的代码。评分者将忽略您的主要方法:

public static int countWords(String original, int minLength){}

您的方法应该计算句子中达到或超过 minLength(以字母计)的单词数。例如,如果给定的最小长度为 4,则您的程序应该只计算长度至少为 4 个字母的单词。

单词将由一个或多个空格分隔。可能存在非字母字符(空格、标点符号、数字等),但不应计入单词长度。

提示:编写一个方法来计算字符串中字母的数量(并忽略标点符号),该字符串包含一个没有空格的单词。在您的 countWords 方法中,将输入字符串分解为单词并将每个单词发送到您的方法。

这是我的代码:

public class WordCount {
    public static void main(String[] args)
    {
        System.out.print("Enter string: ");
        String input = IO.readString();
        System.out.print("Enter minimum length for letter: ");
        int length = IO.readInt();
        IO.outputIntAnswer(countWords(input, length));
    }
    public static int countWords(String original, int minLegth)
    {
        int count = 0;
        int letterCount = 0;
        for(int i = 0; i < original.length(); i++)
        {
            char temp = original.charAt(i);
            if(temp >= 'A' && temp <= 'Z' || temp >= 'a' && temp <= 'z')
            {
                letterCount++;
            }
            else if(temp == ' '|| i == original.length()-1)
            {
                if(letterCount >= minLegth)
                {
                    count++;
                }
                letterCount = 0;
            }
        }
        return count;
    }
}

我的大学使用自动评分器对项目进行评分,但我总是把其中一个测试用例弄错。有人可以帮我找出问题所在吗?

我发现你的代码无法比较最后一个 character.It 的问题需要在最后一个字符之后有一个 space 以便它可以比较最后一个字符,因为 java 没有't use null character terminator for string termination.I 使用 Scanner class 模拟了相同的代码,因为我在使用 io.So 时遇到了一些问题 我做了以下更改:

         Scanner sc1,sc2;
        sc1=new Scanner(System.in);
         String input = sc1.nextLine()+" ";

我不知道是否可以这样做:

String input = IO.readString()+" ";

但我认为你应该尝试在字符串末尾添加空白 space " "