【问题标题】:Find the most vowel words in the sentence (which can be several maximal words that are equivalent to each other)找出句子中元音最多的词(可以是几个最大的词,彼此等价)
【发布时间】:2020-06-21 02:05:30
【问题描述】:

计算方法单词中的元音

public class Methods6 {
 public static int countVowel(String words) {
        int count = 0;
        char[] vowel = {'a', 'e', 'i', 'o', 'u'};
        for (int i = 0; i < words.length(); i++) {
            char ch = words.charAt(i);
            for (char cc : vowel) {
                if (ch == cc) {
                    count++;
                }
            }
        }
        return count;
    }

**查找句子中的最大元音**

public static String maxVowelWords() {
      String sentence = getSentence().toLowerCase();
      String words[] = sentence.split(" ");
      int maxvowel = CountVowel(words[0]), count;
      String maxWord = words[0];
      for (int i = 1; i < words.length; i++) {
          count = CountVowel(words[i]);
          if (count > maxvowel) {
              maxvowel = count;
              maxWord = words[i] + " ";
          }
      }
      return maxWord;

  }}// 2 methods are located  in the same Method Class
public class Test {
    public static void main(String[] args) {
                System.out.println(Methods6.MaxVowelWords());}}

在编写此方法时,它只给出元音字母最多的第一个单词(例如 --------- hello my friend!------------ just你好,你好,朋友-----不! 如何更改此方法? 感谢您的帮助!

【问题讨论】:

  • 它是for 循环,我必须从1 开始。

标签: java arrays string methods static


【解决方案1】:

我对这个问题的解释是:

  • 在提供的字符串中查找包含最多元音的单词 在里面。
  • 如果同一字符串中的一个或多个单词包含相等 最大元音数然后将单词连接在一起 由空格(或其他)分隔。

所以,如果提供的字符串是:"hello my friend",那么两个词,hellofriend,将从 ma​​xVowelWords() 方法以(假设)的形式返回:

hello, friend

由于 hellofriend 都包含 2 个元音,因此恰好是任何给定单词中元音的最大数量,因此返回两个单词。

如果提供的字符串是:"hello my friend - I live on the Mississippi river.",则该方法仅返回 Mississippi,因为它是该字符串中包含最大数量4个元音。字符串中所有其他包含元音的单词都包含较少的元音计数。

如果情况确实如此,那么您的方法应该如下所示:

public static String maxVowelWords() {
    String sentence = getSentence();
    String words[] = sentence.split(" ");
    int maxvowel = 0; 
    String maxWord = "";
    for (int i = 0; i < words.length; i++) {
        int count = countVowels(words[i]);
        if (count == maxvowel) {
            maxvowel = count;
            // Ternary Operator is used here to applt the delimiter (", ") if needed,
            maxWord += (maxWord.equals("") ? words[i] : ", " + words[i]) + " (" + count + " vowels)";
        }
        else if(count > maxvowel) {
            maxvowel = count;
            maxWord = words[i] + " (" + count + " vowels)";;
        }
    }
    return maxWord;
}

是的,你从 0 开始循环,ma​​xWord 字符串变量应该被初始化为保存一个空字符串(“”)。让 for 循环完成它的工作。不,count 不是浪费,它实际上是为了获得 ma​​xvowel 的最大元音计数,因为每个字符串单词都通过该过程放置。

您使用名为 getSentence() 的方法来获取要处理的字符串,并立即通过将原始字符串设置为全部小写来扭曲原始字符串。你真的不应该这样做,因为你从 ma​​xVowelWords() 方法返回的单词不会是最初提供的单词,如果它们碰巧有大写元音。对 countVowel() 方法的小修改可以处理该业务,例如:

if (Character.toLowerCase(ch) == cc) {
    count++;
}

【讨论】:

    【解决方案2】:

    您的代码中几乎没有错误。试试这个:

    public static String maxVowelWords() {
        String sentence = getSentence().toLowerCase();
        String words[] = sentence.split(" ");
        int maxvowel = 0, count;
        String maxWord = "";
        for (int i = 0; i < words.length; i++) {
            count = countVowel(words[i]);
            if (count > maxvowel) {
                maxvowel = count;
                maxWord = "";
            }
            if(count == maxvowel){
                maxWord = maxWord  + words[i]+ " ";
            }
        }
        return maxWord.trim();
    
    }
    

    【讨论】:

    • 如果对您有帮助,请接受并投票。
    • 嗨,你好朋友----如果我给出这句话我得到结果-->朋友
    • 如果您将发现的错误解释为可能会很好。 count 在哪里声明?
    • 谢谢,我在分享这个问题之前试过这个方法,但是当 max.元音词是邻居是结果不符合预期!
    • @UlviIbrahimli 编辑了我的解决方案。覆盖 else-if 到 if。它现在应该可以工作了。
    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2014-12-21
    • 1970-01-01
    相关资源
    最近更新 更多