Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以元音最多的字符串输出单词_Java_Variables_If Statement_Counting_Statements - Fatal编程技术网

Java 以元音最多的字符串输出单词

Java 以元音最多的字符串输出单词,java,variables,if-statement,counting,statements,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

我基本上已经完成了这个方法,但是当涉及到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;  
}       
//元音和辅音的方法
公共静态int元音计数(字符串语句)
{
int maxvouelcount=0;
int minvouelCount=0;
字符串元音=”;
int辅音计数=0;
int-vouelcount=0;
int指数=0;
字符串当前字;
int spacePos;
int noOfchars=0;
int最大=0;
int=0;
//去掉前导空格和尾随空格,以便得到最后一个单词
句子=句子.trim()+“”;
//任务
spacePos=句子索引(“”);
currentword=句子。子字符串(0,空格位置);
while(spacePos>-1)//当找不到空格时
{
noOfchars=0;
currentword=句子。子字符串(0,空格位置);
//删除第一个单词
句子=句子.子字符串(spacePos+1);
spacePos=句子索引(“”);
//计算字符串中元音的数量
对于(索引=0;索引MaxVouelCount)
{
maxvouelcount=vouelcount;
元音=当前单词;
}   
}   
}   
//输出以显示元音数量最大的单词
System.out.println(“元音最多的单词是”+元音);
返回元音计数;
}       

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

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;
}
int-wordStart=0;
int-wordEnd;
while(true){
wordEnd=句子.indexOf(“,wordStart”);
if(wordEnd<0)
打破
字符串字=句子。子字符串(wordStart,wordEnd);
//处理单词
wordStart=wordEnd+1;
}

您是否遗漏了voyel“u”和“y”?在多大程度上您感到困惑?代码有什么问题?请收集额外的信息,然后发布您的帖子。请正确缩进您的代码。对我来说,最让我困惑的地方是if语句用最大/最小值覆盖currentword。对于缩进和信息的缺乏我很抱歉,我是一名编程专业的一年级学生,以前从未做过任何编程。如果你继续边做边编辑代码,那么人们将无法回答这个问题;-)顺便说一句,我不知道if(index==0){minvouelcount=currentword.length();maxvouelcount=currentword.length();}是什么意思。我不知道如何使用say while(有一个词)我应该使用if(currentword.charAt(index)>='a'&¤tword.charAt(index)='A'&¤tword.charAt(索引)请参阅第二个代码段。