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());
    }
}


如果我写书,红色的朋友结果符合预期,但是我写
红皮书的朋友结果是朋友(所以我不会得到2个单词的书和朋友)。
我如何改变这些方法我得到最大。元音词同时
感谢您的帮助!

最佳答案

如果我正确地理解了您的方法,则看起来“足够”足以将else if变成if。但是很可能有更好的算法来完成相同的任务。

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

09-10 07:13
查看更多