如何在 Java 中制作一个简单的元音计数器方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2077457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:17:58  来源:igfitidea点击:

How can I make a simple vowel counter method in Java?

javacounter

提问by Sergio Tapia

Here's my method:

这是我的方法:

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

        }
    }        
}

It says that || cannot be applied to String class. How can I do this then?

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

采纳答案by Roman

char ch = word.charAt (i);
if (ch == 'a' || ch=='e') {

}

回答by Peter Lawrey

Using regular expressions you can try.

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

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

回答by Paul Creasey

    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);

回答by superfav

You can use Peter's code to get the vowels.

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

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

回答by user2162979

This is the way I did it

这是我做的方式

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);
        }
    }
}

回答by Lance Lindsey

Here's what I would have done using Scanner.

这是我会使用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.");
}