有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在字符串中查找元音,并在屏幕上打印元音最多的单词?

我需要找到用户输入字符串中的所有元音,然后在屏幕上打印元音最多的单词
程序使用用户输入
用户以小写形式键入一串单词
e、 g

"I like java programming"

并应宣读:

programming

我试着将字符串拆分成不同的单词,这很有效
我只是不知道如何应用“for”循环来搜索不同的单词。 我需要使用方法,所以这是我用来查找字符串中元音的方法:

public void findvowel(){
    for(int index = 0;index < word.length();index++){
    char vowel = word.charAt(index);
    if( (vowel == 'a')||
        (vowel == 'e')||
        (vowel == 'i')||
        (vowel == 'o')||
        (vowel == 'u')){
            System.out.println(vowel);
            }
        }
    }

但我知道这行不通。 你们能帮我吗


共 (6) 个答案

  1. # 1 楼答案

    您的findVowel()方法就快到了。当你要数数元音的时候,为什么还要输出元音呢?我想你需要的不是findVowel(),而是一个名为countVowels()的东西,它可以返回一个单词中的元音数量。比如:

        public int countVowels(String word){
          int count = 0;
          for(int index = 0;index < word.length();index++){
            char vowel = word.charAt(index);
            if( (vowel == 'a')||
                    (vowel == 'e')||
                    (vowel == 'i')||
                    (vowel == 'o')||
                    (vowel == 'u')){
                count++;
            }
          }
          return count;
        }
    

    通过这种方式,你可以对句子中的每个单词调用countVowels(),并跟踪到目前为止元音最多的单词。 例:

    String sentence = "This is a sentence.";
    String[] words = sentence.split(" ");  //splits sentence into string array by spaces
    
    String maxStringSoFar = "";
    int maxStringVowelCount = 0;
    for(String s : words)
    {
         int currentWordVowelCount = countVowels(s);
         if(currentWordVowelCount > maxStringVowelCount)
         {
              maxStringSoFar = s;
              maxStringVowelCount = currentWordVowelCount;
         }
    }
    
  2. # 2 楼答案

    你走的方向是对的。几件事:

    你不需要打印元音。你将计算所有单词中元音的数量。当你一次只做一个单词时,你要记住前面的单词的计数。更好的策略是只记住元音最多的单词。每当你发现一个单词有更多元音,你就会更新你的结果

    您可以使用字段来记住最大元音数的单词以及数字:

    String wordWithMaxVowels;
    int maxNumberOfVowels;
    

    假设在这个例子中,您正在处理一个word。你需要一个local变量来保持这个^{中元音的计数

    int vowelCount = 0;
    // Your code to loop over the word but remember
    // to increase vowelCount if you find a vowel:
    // vowelCount++;
    

    最后检查一下,看看这个数字是否大于目前为止的最大值。如果出现这种情况,请更新字段:

    if(vowelCount > maxNumberOfVowels) {
        wordWithMaxVowels = word;
        maxNumberOfVowels = vowelCount;
    }
    

    另一个提示如下。要检查字符c是否为元音,可以:

    if ("aeiouAEIOU".indexOf(c) != -1) {
        vowelCount++;
    }
    
  3. # 3 楼答案

    public class Test2{
    public static void main(String[] args) {
        String message = "words containig mooooost vowels";
        String wordWithMostVowel = null;
        int maxVowelCount = 0;
        String tests[] = message.split(" ");
        int totalVowel = 0;
        for(String test : tests){
            int vowelCount = 0;
            test = test.toLowerCase();
            for(int i=0;i<test.length();i++){
                switch(test.charAt(i)){
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                vowelCount++;
                }
                if(vowelCount > maxVowelCount){
                    maxVowelCount = vowelCount;
                    wordWithMostVowel = test;
                }
            }
            totalVowel = totalVowel+vowelCount;
        }
        System.out.println("total vowels "+totalVowel+" word with max vowel is "+wordWithMostVowel);
    }
    

    }

  4. # 4 楼答案

    public class MaxVowels {
        public static void main(String[] args) {
            String sentence = "This is a loooooooooong sentence";
            int maxVowelCount = 0;
            String wordsWithMostVowels = null;
            String[] words = sentence.split(" ");
    
            for(String word : words){
                int vowelCount = 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'){
                        vowelCount++;
                    }
                }
                if(vowelCount > maxVowelCount){
                    maxVowelCount = vowelCount;
                    wordsWithMostVowels = word;
                }
            }
            System.out.println("Word with most vowels is: " + wordsWithMostVowels);
        }
    }  
    

    代码相当简单,无需解释=)
    代码忽略了两个单词元音数相同的情况。在这种情况下,第一个单词将用作大多数元音的单词

  5. # 5 楼答案

    这应该是一个很好的起点;请注意,方法名称现在真正说明了它的功能:

    // public static int findvowel(String word) {
    public static int getVowelCount(String word) {
      int count = 0;
      if (word != null) {
        word = word.trim().toLowerCase();
      }
      if (word == null || word.length() < 1) {
        return count;
      }
      for (int index = 0; index < word.length(); index++) {
        // That Fred he's a
        char fred = word.charAt(index);
        if ((fred == 'a') || (fred == 'e')
            || (fred == 'i') || (fred == 'o')
            || (fred == 'u')) {
          ++count;
          }
      }
      System.out.println("For the word \"" + word
          + "\" there are " + count + " vowels");
      return count;
    }
    
  6. # 6 楼答案

    嘿,我有一些不同的答案------

    class strDemo2
    {
        public static void main(String args[])
        {
        String s1=new String("The Ghost of The Arabean Sea");
    
    
            char c1[]=new char[30];
            char c2[]={'a','e','i','o','u'}; 
             s1.getChars(0,28,c1,0);
             int pf=0,pl=0;
            for(int i=0;i<s1.length();i++)
            {
               for(int j=0;j<5;j++) 
               {
                  if(c1[i]==c2[j])   
                  {
              System.out.println("vowel found at:-> "+(i+1)+" which is "+c2[j]);
                  }    
               }  
            }               
    
       }
    }