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

如何在Java中创建一个简单的元音计数器方法?,java,counter,Java,Counter,以下是我的方法: public char[] ReturnAllVowels(String word) { for (int i = 0; i < word.length(); i++) { if (word.contains("a" || "e" || "i" || "o" || "u")) { } } } public char[]返回所有元音(字符串字) { for(int i=0;

以下是我的方法:

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

        }
    }        
}
public char[]返回所有元音(字符串字)
{
for(int i=0;i

它说| |不能应用于字符串类。那我怎么做呢?

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

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

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

你可以使用彼得的密码来获取元音

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

我就是这样做的

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);
        }
    }
}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
//此处的TODO代码应用程序逻辑
字符串s;
//字符串元音=a;
扫描仪输入=新扫描仪(系统输入);
s=in.nextLine();

对于(inti=0;i,下面是我使用
扫描仪时应该做的事情

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.");
}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
字符串用户输入;
int-vouela=0,vouele=0,voueli=0,vouelo=0,vouelu=0;
System.out.println(welcomeMessage);
userInput=scan.nextLine();
userInput=userInput.toLowerCase();

对于(int x=0;x),Java中的方法名称应为小写(
returnallvowells
)。您的问题是希望获取
元音计数器
,但函数返回
char[]
。你想要一个计数器还是想要包含实际的元音?包含实际的元音,我不需要从小写字母开始。这只是品味的问题。每个人都有自己的写作方式。品味就像选择在新行上加上大括号一样,你的工作是编写人们能够理解的代码,标识符开头的大写字母通常表示(在Java中)类名而不是方法名。同样,以小写字母开头是一个古老的约定,如果不遵循上述约定,许多基于反射的工具可能会崩溃。嘿,小错误,请至少留下注释。
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.");
}