【发布时间】: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