【问题标题】:Find the most vowel words in the sentence (which can be several maximal words that are equivalent to each other and are located neighbor)找到句子中元音最多的词(可以是几个最大的词,彼此等价,并且位于相邻)
【发布时间】:2020-06-21 03:40:32
【问题描述】:
public class Methods6 {

    public static String getSentence() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("cumleni daxil et:");
        String sentence = scanner.nextLine();
        return sentence;
    }

    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 = "";
        for (int i = 0; i < words.length; i++) {
            count = CountVowel(words[i]);
            if (count >maxvowel) {
                maxvowel = count;
                maxWord = "";
            }
            else if(count >= maxvowel){
                maxWord = maxWord  + " " +words[i];
            }
        }
        return maxWord;
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        System.out.println(Methods6. maxVowelWords());
    }
}

如果我写书红朋友结果如预期,但我写 red book friend 结果 friend(所以我没有得到 2 个字 bookfriend)。
我如何改变这些方法我得到最大。元音词同时, 感谢您的帮助!

【问题讨论】:

  • 您是否尝试过调试您的代码?这将是查看算法问题的最简单方法。
  • 删除maxword = word[i]的评论

标签: java arrays string methods static


【解决方案1】:

如果我正确理解您的方法,看起来将“有点”足以将else if 仅转换为if。但很可能有更好的算法来完成相同的任务。

'有点',因为很多时候你的结果开头会有“”(空白)。

【讨论】:

  • 是的,我总是得到“”(空白)每个结果。但我没有其他算法。谢谢帮助。
【解决方案2】:

您的maxVowelWords 方法有一个小错误。

if (count >maxvowel) {
    maxvowel = count;
//  maxWord=words[i];
    maxWord = "";
}

如果单词的元音比最后一个单词多,它会将maxWord 设置为""。 如示例 red 有一个元音,现在它检查 book 这有 2 个元音它进入 if 情况并设置 maxvowel 计数为 2,但您设置了 maxWord = ""

接下来你看一下friend这个词,它也有两个元音,然后你进入另一种情况

else if(count >= maxvowel){
    maxWord = maxWord  + " " +words[i];
}

这会将朋友添加到输出中,但朋友仍然会丢失。

你可能想要这样的东西:

if (count >maxvowel) {
    maxvowel = count;
    maxWord=words[i];
}

这会将您的 maxWord 设置为具有最多元音的当前单词。

【讨论】:

  • 写入时 if (count >maxvowel) { maxvowel = count; maxWord=单词[i]; } 我得到第一个 max.vowel 两次
【解决方案3】:

你也可以考虑使用流 API 来解决这个问题:

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static java.util.Comparator.naturalOrder;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


  static int countVowels(String word) {
    int count = 0;
    for (char c : word.toCharArray()) {
      if ("aeiou".indexOf(c) >= 0) {
        count++;
      }
    }
    return count;
  }

  public static String maxVowelWords() {
    String sentence = getSentence().toLowerCase();
    Map<Integer, List<String>> wordsByVowelCount = Arrays.stream(sentence.split(" "))
        .collect(groupingBy(Methods6::countVowels));
    int maxVowelCount = wordsByVowelCount.keySet().stream().max(naturalOrder()).orElse(0);
    return wordsByVowelCount.get(maxVowelCount).stream().collect(joining(" "));
  }

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2023-03-04
    相关资源
    最近更新 更多