【发布时间】: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 个字 book 和 friend)。
我如何改变这些方法我得到最大。元音词同时,
感谢您的帮助!
【问题讨论】:
-
您是否尝试过调试您的代码?这将是查看算法问题的最简单方法。
-
删除
maxword = word[i]的评论
标签: java arrays string methods static