在具有最大元音量的字符串中输出单词

时间:2014-12-29 16:45:24

标签: java variables if-statement counting statements

我主要完成了这个方法,但是当涉及到存储一个带有元音量最多的单词的变量的If语句时,我很困惑。注意我不允许使用数组。 下面是我的代码

//method for vowels and consonants

public static int Vowelcount(String sentence)
{

    int maxvowelcount =0;
    int minvowelcount =0;
    String vowel="";
    int consonantcount = 0;
    int vowelcount = 0;
    int index = 0;
    String currentword;
    int spacePos;
    int noOfchars = 0;
    int largest = 0 ;
    int smallest = 0 ;

    //gets rid of leading and trailing spaces so as to get the last word
    sentence=sentence.trim() + " ";     

    //assignments
    spacePos=sentence.indexOf(" ");
    currentword = sentence.substring(0, spacePos);

    while (spacePos >-1)// when no spaces are found
    {
        noOfchars=0;
        currentword = sentence.substring(0, spacePos);

        // remove the first word
        sentence = sentence.substring(spacePos+1);

        spacePos=sentence.indexOf(" ");

        // to count the number of vowels in the string 
        for(index = 0; index<currentword.length(); index++)
        {
            if(currentword.charAt(index)=='a' || currentword.charAt(index)=='e' ||
               currentword.charAt(index)=='i' || currentword.charAt(index)=='o' ||
               currentword.charAt(index)=='A' || currentword.charAt(index) == 'E' ||
               currentword.charAt(index) == 'I' || currentword.charAt(index) == 'O' )
            {
                vowelcount++;
            }            
            else
            {
                consonantcount++;
            }       

            //if statement to overwrite currentword with largest/smallest
            if (index == 0)
            {
                minvowelcount = currentword.length();
                maxvowelcount = currentword.length();
            }

            if (vowelcount < minvowelcount)
            {
                minvowelcount = vowelcount;
            }           

            if (vowelcount > maxvowelcount)
            {
                maxvowelcount = vowelcount;
                vowel=currentword;
            }   
        }   
    }   

    //output to show the word with largest amount of vowels
    System.out.println( "word with largest amount of vowels is " + vowel);
    return vowelcount;  
}       

1 个答案:

答案 0 :(得分:2)

首先,您使用的许多变量都是多余的。您所需要的只是元音和单词的最大数量。 接下来的一点是,所有if语句都应该在for循环之外。一般结构必须像

一样
wordWithMaxVowels = "";
maxVowels = 0;
while (there is a word) {
    fetch the word
    vowelCount = 0;
    for (every char in the word) 
    {
        if (char is vowel)
            vowelCount++;
    }
    if (vowelCount > maxVowels)
    {
         vowelCount = maxVowels;
         wordWithMaxVowels = word;
    }
}
// here maxVowels is the maximal number of vowels in a word, wordWithMaxVowels is the word itself;

另请注意,切断句子中的第一个单词并不是最佳选择。请尝试以下方法:

int wordStart = 0;
int wordEnd;
while(true) {
    wordEnd = sentence.indexOf(" ", wordStart);
    if (wordEnd < 0)
        break;
    String word = sentence.substring(wordStart, wordEnd);

    // process the word

    wordStart = wordEnd + 1;
}