2

这是我的方法:

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

        }
    }        
}

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

4

6 回答 6

12

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

int count = word.replaceAll("[^aeiouAEIOU]","").length();
于 2010-01-16T13:37:07.710 回答
1
char ch = word.charAt (i);
if (ch == 'a' || ch=='e') {

}
于 2010-01-16T13:33:55.713 回答
1
    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);
于 2010-01-16T13:36:25.107 回答
0

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

char[] vowels = word.replaceAll("[^aeiouAEIOU]","").toCharArray();
于 2010-01-16T14:39:52.857 回答
0

我就是这样做的

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);
        }
    }
}
于 2013-03-12T21:36:47.580 回答
0

这是我使用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-10-01T00:00:46.273 回答