用于打印具有大量元音的单词的程序

时间:2016-07-05 20:07:14

标签: java arrays string

我无法弄清楚这段代码有什么问题。无论我提供什么作为输入,代码打印i作为输出。任何帮助表示赞赏。

public class VowelClass{

public static void main(String args[]){

String input;
System.out.println("Please enter your sentence: ");

Scanner scan = new Scanner(System.in);
input = scan.nextLine();

int maxVCount = 0;
String mostVowels = null;
String[] words = input.split("");

for (String word : words) {

  int vCount = 0;
  word = word.toLowerCase();

  for (int i=0; i<word.length(); i++){
    char x = word.charAt(i);
    if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'){
    vCount++;
    }
  }
   if (vCount > maxVCount) {
    maxVCount = vCount;
    mostVowels = word;
   }
  }
    System.out.println("Word with most vowels is:");
    System.out.println(mostVowels);
  }
}

2 个答案:

答案 0 :(得分:1)

input.split(""); =&gt; input.split(" ");

您需要在单词之间进行拆分,因此请使用拆分方法中的“空格”字符。

答案 1 :(得分:0)

class sen_wo
{

    String sen;

    void inputString(String sen1)
    {
        sen=sen1;
        int l=0,noc=0,wctr=0;
        char ch=' ';
        String wo="";
        l=sen.length();
        String word[]=new String[l];
        for(noc=0;noc<l;noc++)
        {
            ch=sen.charAt(noc);
            if(ch!=' ')
                wo+=ch;
            else
            {
                word[wctr++]=wo;
                wo="";
            }
        }
        word[wctr]=wo;
        int wctr1=0,cctr=0;
        String wor="";

        for(wctr1=0;wctr1<=wctr;wctr1++)
        {
            String bw=word[wctr1];
            int ctr=0;
            for(noc=0;noc<bw.length();noc++)
            {
                char ch1=bw.charAt(noc);
                if(ch=='A' ||ch=='U' ||ch=='I' ||ch=='E' ||ch=='O')
                    ctr++;
            }

            if(ctr>cctr)
            {
                cctr=ctr;
                wor=word[wctr1];
            }
        }
        System.out.print(wor+"is having maximum no of consonant");
    }
}
相关问题