【问题标题】:How can I make a simple vowel counter method in Java?如何在 Java 中制作一个简单的元音计数器方法?
【发布时间】:2010-01-16 13:32:38
【问题描述】:

这是我的方法:

public char[] ReturnAllVowels(String word)
{
    for (int i = 0; i < word.length(); i++)
    {
        if (word.contains("a" || "e" || "i" || "o" || "u"))     
        {

        }
    }        
}

它说||不能应用于 String 类。那我该怎么做呢?

【问题讨论】:

  • 你的方法名在 Java 中应该是小写的 (returnAllVowels)。你的问题说你想得到vowel counter,但你的函数返回char[]。你想要一个计数器还是想要包含实际的元音?
  • 实际包含的元音,我真的不必以小写开头。这只是口味问题。每个人都有自己的写作方式。
  • 品味就像选择将左大括号放在新行上,您的工作是编写人们可以理解的代码,标识符开头的大写通常表示(在Java中)类名而不是而不是方法名。
  • 同样,以小写字母开头是一个古老的约定,如果您不遵循上述约定,有很多特别基于反射的工具可能会中断。

标签: java counter


【解决方案1】:

使用正则表达式你可以试试。

int count = word.replaceAll("[^aeiouAEIOU]","").length();

【讨论】:

    【解决方案2】:
    char ch = word.charAt (i);
    if (ch == 'a' || ch=='e') {
    
    }
    

    【讨论】:

    • 嘿,矿工们,至少离开 cmets。
    【解决方案3】:
        String regex = "[aeiou]";               
        Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);   
        int vowelcount = 0;
        Matcher m = p.matcher(content);
        while (m.find()) {
          vowelcount++;
        }
        System.out.println("Total vowels: " + vowelcount);
    

    【讨论】:

      【解决方案4】:

      您可以使用 Peter 的代码来获取元音。

      char[] vowels = word.replaceAll("[^aeiouAEIOU]","").toCharArray();
      

      【讨论】:

        【解决方案5】:

        我就是这样做的

        public static void main(String[] args) {
            // TODO code application logic here
        
            // TODO code application logic here
            String s;
            //String vowels = a;
            Scanner in = new Scanner(System.in);
            s = in.nextLine();
        
            for(int i = 0; i<s.length();i++){
                char v = s.charAt(i);
                if(v=='a' || v=='e' || v=='i' || v=='o' || v=='u' || v=='A' || v=='E' || v=='I' || v=='O' || v=='U'){
                    System.out.print (v);
                }
            }
        }
        

        【讨论】:

          【解决方案6】:

          这是我使用 Scanner 会做的事情。

          public static void main(String[] args) {
              Scanner scan = new Scanner(System.in);
          
              String userInput;
              int vowelA = 0, vowelE = 0, vowelI = 0, vowelO = 0, vowelU = 0; 
          
              System.out.println(welcomeMessage);
              userInput = scan.nextLine();
              userInput = userInput.toLowerCase();
          
              for(int x = 0; x <= userInput.length() - 1; x++) {
                  if(userInput.charAt(x) == 97)
                      vowelA++;
                  else if(userInput.charAt(x) == 101)
                      vowelE++;
                  else if(userInput.charAt(x) == 105)
                      vowelI++;
                  else if(userInput.charAt(x) == 111)
                      vowelO++;
                  else if(userInput.charAt(x) == 117)
                      vowelU++;   
              } 
          
              System.out.println("There were " + vowelA + " A's in your sentence.");
              System.out.println("There were " + vowelE + " E's in your sentence.");
              System.out.println("There were " + vowelI + " I's in your sentence.");
              System.out.println("There were " + vowelO + " O's in your sentence.");
              System.out.println("There were " + vowelU + " U's in your sentence.");
          }
          

          【讨论】:

            猜你喜欢
            • 2013-11-10
            • 1970-01-01
            • 2018-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多