【问题标题】:using scanner to evaluate words in a string使用扫描仪评估字符串中的单词
【发布时间】:2014-11-04 21:38:39
【问题描述】:

我正在尝试编写一个方法,该方法从“words”参数中返回至少具有“min”但不超过“max c”字符的单词数。

public static int countWords(String words, int min, int max)
{
    Scanner s = new Scanner (words);
    int counter = 0;

 while  (s.hasNext())
    {
    String word = s.next();
    int wordLength = word.length();
    if (wordLength>min && wordLength<max)
    {
        counter= counter + 1;
        s.close();
        }
    }
    return counter;
}

【问题讨论】:

  • 我完全不清楚你在问什么。你的英文措辞很混乱。问题是什么?错误在哪里?
  • 你能帮我翻译一下吗? “我正在尝试编写一种方法,该方法从“words”参数中返回至少具有最小字符但不超过“最大 c”字符的单词数。”
  • 大声笑,我复制并粘贴了说明,就像我被告知的那样。但是我试图在字符串(单词)中找到至少有一定数量的字符(min)并且不超过一定数量的字符(max)的单词。希望这更有意义
  • 这里有什么问题?
  • 我的导师给出了测试用例,根据这些测试用例有问题,所以我希望能得到一些帮助

标签: java java.util.scanner


【解决方案1】:

只是一个小小的改变。您正在 while 循环中关闭扫描仪,将其移出循环即可。

public static int countWords(String words, int min, int max) {
        Scanner s = new Scanner(words);
        int counter = 0;
        while (s.hasNext()) {
            String word = s.next();
            int wordLength = word.length();
            if (wordLength > min && wordLength < max) {
                counter = counter + 1;
            }
        }
        s.close();
        return counter;
    }

我还建议您更改 if 条件以使其包含 min 和 max。 if (wordLength >= min && wordLength =

【讨论】:

    【解决方案2】:

    之前在

    中讨论过在处理字符串时使用拆分与扫描器的偏好

    我更喜欢正则表达式,所以这里有一个使用String的split方法的解决方案如下:

    使用正则表达式

    public class Sample {
        
        public static int countWords(String words, int min, int max) {
    
            String[] allWords = words.split("(?s)\\s+");
            int counter = 0;
    
            for (String word : allWords) {
                if(word.length() >= min && word.length() <= max)
                    counter++;
            }
            return counter;
        }
        
        public static void main(String[] args) {
            String s = "find all the long words in this sentence";
            System.out.println(countWords(s,4,7));
        }
    }
    

    如果你想使用Scanner,你可以这样做:

    使用扫描仪

    public class Sample {
        
        public static int countWords(String words, int min, int max) {
    
            Scanner scan = new Scanner(words);
            int counter = 0;
            int length = 0;
    
            while(scan.hasNext()){
                length  = scan.next().length();
                if(length >= min && length <= max)
                    counter++;
            }
            scan.close();
            return counter;
        }
        
        public static void main(String[] args) {
            String s = "find all the long words in this sentence";
            System.out.println(countWords(s,4,7));
        }
    }
    

    【讨论】:

      【解决方案3】:

      根据我对你的理解,你想计算指定了 max 和 min 的单词

      例如,

      "The rain in Spain falls mainly in the plain", 3, 5 是返回 5 因为它们是 3 个五和 2 个三

      你的代码没什么问题,除了这部分需要改变它的逻辑

      if (wordLength>min && wordLength<max) 
      

      根据我从您的问题中了解到的,您想要具有最小长度或最大长度的单词

      因此,您必须将 if 语句更改为此

      if (wordLength == min || wordLength == max) {
      

      说明:单词的长度等于最小长度或最大长度,将添加计数器

      【讨论】:

        猜你喜欢
        • 2019-07-25
        • 1970-01-01
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多